Laravel 5.5中如何在请求参数存在的情况下添加条件

时间:2019-07-09 09:48:59

标签: laravel-5.5

如果Laravel 5.5中存在请求参数,如何添加条件 假设:

$a = $request->name;
$b = $request->id;
$c = $request->option;
$d = $request->type;

$cities = Pocities::where('region_id', $state_id)
                    ->where('is_active', 1)
                    ->pluck('uuid')->toArray();

$tours = Tour::select(['id', 'uuid','city_uuid', 'tour_name', 'tour_image', 'is_top_tour','places', 'created_at'])
                ->with(['pocity' => function ($query) {
                    $query->select('id', 'uuid', 'cities_id', 'region_id', 'country_id');
                },'pocity.city','pocity.poregion' ,'pocity.pocountry'])
                ->whereHas('pocity',function ($query) use ($cities) {
                    $query->whereIn('uuid',  $cities);
                })->where('is_active');


if($request->name != ''){
    $tours->where('name', $request->name);
}

if($request->id != ''){
    $tours->where('id', $request->id);
}

if($request->type != ''){
    $tours->where('type', $request->type);
}

$tours->paginate(10);

我每次都尝试过

#query: Builder {#1944
    +connection: MySqlConnection {#621
      #pdo: PDOConnection {#1926
        inTransaction: false
        attributes: {
          CASE: NATURAL
          ERRMODE: EXCEPTION
          AUTOCOMMIT: 1
          PERSISTENT: false
          DRIVER_NAME: "mysql"
          SERVER_INFO: "Uptime: 980  Threads: 7  Questions: 420  Slow queries: 0  Opens: 24  Flush tables: 1  Open tables: 18  Queries per second avg: 0.428"
          ORACLE_NULLS: NATURAL
          CLIENT_VERSION: "mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387 $"
          SERVER_VERSION: "5.5.5-10.2.14-MariaDB"
          STATEMENT_CLASS: array:2 [
            0 => "Doctrine\DBAL\Driver\PDOStatement"
            1 => []
          ]
          EMULATE_PREPARES: 0
          CONNECTION_STATUS: "127.0.0.1 via TCP/IP"
          DEFAULT_FETCH_MODE: BOTH
        }
      }

2 个答案:

答案 0 :(得分:0)

如果上述方法不适合您,则可以执行以下操作。

$tours = Tour::query();
//Add your conditions here
$tours->->with(['pocity' => function ($query) {
                $query->select('id', 'uuid', 'cities_id', 'region_id', 'country_id');
            },'pocity.city','pocity.poregion' ,'pocity.pocountry'])
            ->whereHas('pocity',function ($query) use ($cities) {
                $query->whereIn('uuid',  $cities);
            })->where('is_active')
//Add your conditionally  parameters in this way.
if($request->name != ''){
$tours->where('name', $request->name);
}

$tours->paginate(10);

我假设您要传递的列名正确。

答案 1 :(得分:0)

您可以在laravel5.5中查询builder的where子句

$a = $request->name;
$b = $request->id;
$c = $request->option;
$d = $request->type;

$cities = Pocities::where('region_id', $state_id)
                    ->where('is_active', 1)
                    ->pluck('uuid')->toArray();
$tours = Tour::select(['id', 'uuid','city_uuid', 'tour_name', 'tour_image', 'is_top_tour','places', 'created_at'])
                    ->with(['pocity' => function ($query) {
                        $query->select('id', 'uuid', 'cities_id', 'region_id', 'country_id');
                },'pocity.city','pocity.poregion' ,'pocity.pocountry'])
                ->whereHas('pocity',function ($query) use ($cities) {
                    $query->whereIn('uuid',  $cities);
                })->where('is_active');

if($request->name != '' && $request->id != '' && $request->type != ''){
$tours = Pocities::query();
$tours->where('name', $request->name);
$toursData = $tours->paginate(10);

}