ashok poudel
ashok poudel

Reputation: 723

pass where clause as parameter to another function laravel

I have a method which handles the query processing from the data table.

Right now the function looks like this:

   $rowsPerPage = $request->get('rowsPerPage') ? $request->get('rowsPerPage') : 10;
    $sortBy = $request->get('sortBy');
    $desc = $request->get('descending');
    $query = $request->get('query');

    $modelData = Airline::where('id', '>', 0);
    if ($query) {
        $modelData = $modelData->where(function ($q) use ($query) {
            $q->where('airline_name', 'LIKE', '%' . $query . '%')
            ->where('iata_code', 'LIKE', '%' . $query . '%');
        })->orWhereHas('country', function ($q) use ($query){
            $q->where('country_name', 'LIKE', '%' . $query . '%');
        });
    }


    if ($sortBy == 'null') {
        $sortBy = 'created_at';
        $desc = false;
    }
    if ($desc) {
        $modelData->orderBy($sortBy, 'DESC');
    } else {
        $modelData->orderBy($sortBy, 'ASC');
    }
    $rowsPerPage = $rowsPerPage==-1 ? $modelData->count() : $rowsPerPage;

    $modelData = $modelData->paginate($rowsPerPage);

    return  $modelData;

This is working fine but I am repairing the same code again in another controller method. The only thing that changes between the controller is the whereClause method. I want to put all the code in this method to another function except for this part.

 $modelData->where(function ($q) use ($query) {
        $q->where('airline_name', 'LIKE', '%' . $query . '%')
        ->where('iata_code', 'LIKE', '%' . $query . '%');
    })->orWhereHas('country', function ($q) use ($query){
        $q->where('country_name', 'LIKE', '%' . $query . '%');
    });

I want to pass this where clause as a parameter to a new function, for example:

public static function IndexQuery($model,$whereClause){

    $rowsPerPage = request('rowsPerPage')? request('rowsPerPage') : 10;
    $sortBy = request('sortBy');
    $desc = request('descending');
    //Where clause pass should execute here with model object .
   if ($sortBy == 'null') {
        $sortBy = 'created_at';
        $desc = false;
    }
    if ($desc) {
        $model->orderBy($sortBy, 'DESC');
    } else {
        $model->orderBy($sortBy, 'ASC');
    }
    $rowsPerPage = $rowsPerPage==-1 ? $model->count() : $rowsPerPage;

    $data = $model->paginate($rowsPerPage);
    return $data;
}   

Can anyone help me with this problem?

Upvotes: 0

Views: 803

Answers (2)

Vikash Pathak
Vikash Pathak

Reputation: 3562

You can use Laravel Model Scope for this. Try using Model Local Scope like:

// inside you model Airline.php

public function scopeFilterWithQuery($queryBuilder, $query)
{
    if($query) {

        $queryBuilder = $queryBuilder->where(function ($q) use ($query) {
            $q->where('airline_name', 'LIKE', '%' . $query . '%')
            ->where('iata_code', 'LIKE', '%' . $query . '%');
        })->orWhereHas('country', function ($q) use ($query){
            $q->where('country_name', 'LIKE', '%' . $query . '%');
        });
    }

    return $queryBuilder;
}

// Now in controller

$modelData = Airline::where('id', '>', 0)->filterWithQuery($query);

// your other codes.

You can wrap as much as common conditions in model scope and reuse that.

Upvotes: 0

Julius Fasema
Julius Fasema

Reputation: 902

try this...

public function WhereClause($q,$query)
{
 $qdata= $modelData->where(function ($q) use ($query) {
        $q->where('airline_name', 'LIKE', '%' . $query . '%')
        ->where('iata_code', 'LIKE', '%' . $query . '%');
    })->orWhereHas('country', function ($q) use ($query){
        $q->where('country_name', 'LIKE', '%' . $query . '%');
    });
  return $qdata;
}

//controller

$qresult=$this->WhereClause($data1, $data2);
public static function IndexQuery($model,$qresult){

    $rowsPerPage = request('rowsPerPage')? request('rowsPerPage') : 10;
    $sortBy = request('sortBy');
    $desc = request('descending');
    //Where clause pass should execute here with model object .
   if ($sortBy == 'null') {
        $sortBy = 'created_at';
        $desc = false;
    }
    if ($desc) {
        $model->orderBy($sortBy, 'DESC');
    } else {
        $model->orderBy($sortBy, 'ASC');
    }
    $rowsPerPage = $rowsPerPage==-1 ? $model->count() : $rowsPerPage;

    $data = $model->paginate($rowsPerPage);
    return $data;
}   

Upvotes: 1

Related Questions