如何在laravel中构建单个查询

时间:2017-08-18 09:49:09

标签: laravel

Table Image

$tadaydate = ddate('m/d/Y');

$value1 = value_table::where('value_1', $tadaydate)->get();

$value2 = value_table::where('value_2', $tadaydate)->get();

$value3 = value_table::where('value_3', $tadaydate)->get();

$value4 = value_table::where('value_4', $tadaydate)->get();

$value5 = value_table::where('value_5', $tadaydate)->get();

有没有机会查看单个查询?

4 个答案:

答案 0 :(得分:0)

您可以使用我认为的“ - > whereIn()”函数。

$value_array = ['value1',...,'value5'];
$value_table->whereIn($tadaydate, $value_array)->get;

答案 1 :(得分:0)

你应该试试这个:

$tadaydate=ddate('m/d/Y');
$result = value_table::where('value_1',$tadaydate)
                      ->orWhere('value_2',$tadaydate)
                      ->orWhere('value_3',$tadaydate)
                      ->orWhere('value_4',$tadaydate)
                      ->orWhere('value_5',$tadaydate)
                      ->get();

更新了答案

$user_list = value_table::where(function($query) use ($tadaydate){
        $query->where('value_1', =, $tadaydate);
        $query->where('value_2', =, $tadaydate);
        $query->where('value_3', =, $tadaydate);
        $query->where('value_4', =, $tadaydate);
        $query->where('value_5', =, $tadaydate);
    })
    ->get();

希望这对你有用!!!

答案 2 :(得分:0)

您可以合并:

$query = value_table::where('value_1',$tadaydate)
         ->orWhere('value_2',$tadaydate)
         ->orWhere('value_3',$tadaydate)
         ->orWhere('value_4',$tadaydate)
         ->orWhere('value_5',$tadaydate)
         ->get()

您应该使用where代替orWhere来连接带有AND而不是OR

的人

要在循环中简化它:

$columns = 7;
$query = value_table::query();
for ($i = 1;$i <= $columns;$i++) {
     $query = $query->orWhere('value_'.$i,$tadaydate);
} 
$values=$query->get();

但是,如果规范化表格,您将获得更多成功:

 table 
 -----------
 id | more columns

 value_table
 ------------
 id | table_id (fK) | value

两个模型TableValueTable。这样(假设适当的雄辩设置)你可以这样做:

 $tableRow = Table::whereHas("value", function ($q) use ($tadaydate) {
       return $q->where("value",$tadaydate);
 })->get();

答案 3 :(得分:0)

您可以使用whereIn。在whereIn中,您可以像这样传递数组:

 $values_list = [$val1,$val2,$val3];
 $data = YourModel::whereIn($tadaydate, $values_list)->get();