Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在新行上编写laravel sql查询子句_Php_Mysql_Laravel - Fatal编程技术网

Php 如何在新行上编写laravel sql查询子句

Php 如何在新行上编写laravel sql查询子句,php,mysql,laravel,Php,Mysql,Laravel,在codeigniter中,我使用“;”在单独的行上编写查询用分隔符号代替'->'符号, 例如: 和我在拉雷维尔想要的一样。当我选择颜色时,应该应用color where子句 $query = DB::table('product') if($color){ ->whereColor($color) } ->get(); 但其显示错误语法错误,意外的“if” 如何在laravel中编写这样的查询?方法链接返回查询对象。因此,您可以使用$query变量 $query = DB::

在codeigniter中,我使用“;”在单独的行上编写查询用分隔符号代替'->'符号, 例如:

和我在拉雷维尔想要的一样。当我选择颜色时,应该应用color where子句

$query = DB::table('product')
if($color){
  ->whereColor($color)
}
->get();
但其显示错误语法错误,意外的“if”
如何在laravel中编写这样的查询?

方法链接返回查询对象。因此,您可以使用$query变量

$query = DB::table('product');
if($color){
  $query->whereColor($color);
}
$query->get();

别忘了放一张卡片;请在每行末尾添加where子句,如$query->where'color','$color'

  $color = 'red';
    $query = DB::table('product');
    if($color){
      $query->where('color', '$color');
    }

    $rows = $query->get();
============ 另一种方式是:

 $rows = Product::where_color('$color')->get();

你需要链,没有对象你不能使用->操作符: 尝试使用$query varabile链接,如下所示:

$query = DB::table('product'); // create instance
 if($color){
   $query->whereColor($color); // chain here
 }
 $query = $query->get(); // get result
$query = DB::table('product'); // create instance
 if($color){
   $query->whereColor($color); // chain here
 }
 $query = $query->get(); // get result