Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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雄辩搜索多个领域和关系_Php_Laravel_Laravel Eloquent - Fatal编程技术网

Php Laravel雄辩搜索多个领域和关系

Php Laravel雄辩搜索多个领域和关系,php,laravel,laravel-eloquent,Php,Laravel,Laravel Eloquent,我正在尝试创建一个搜索查询,以便在几个不同的字段中搜索相同的字符串。查询还需要从machine\u中搜索字段,该字段在Machines模型上设置为hasOne关系 这是机器型号中的保修关系 public function warranty() { return $this->hasOne('App\MachineWarranty', 'machine_id'); } 下面是我对这个问题的尝试。搜索序列或机器类型很好,但我不知道如何也搜索关系 Machine::orderBy('c

我正在尝试创建一个搜索查询,以便在几个不同的字段中搜索相同的字符串。查询还需要从
machine\u
中搜索字段,该字段在
Machines
模型上设置为hasOne关系

这是机器型号中的保修关系

public function warranty()
{
    return $this->hasOne('App\MachineWarranty', 'machine_id');
}
下面是我对这个问题的尝试。搜索序列或机器类型很好,但我不知道如何也搜索关系

Machine::orderBy('created_at', 'desc')
  ->where('serial', 'LIKE', '%' . $search . '%')
  ->orWhere('type', 'LIKE', '%' . $search . '%')
  ->with(['warranty' => function($query) use ($search) {
       $query->where('first_name', 'LIKE', '%' . $search . '%');
  }]);
有没有一种方法可以执行
->或使用()
也许?

使用以下方法:

使用以下方法:

使用
whereHas()
进行
查询:

Machine::orderBy('created_at', 'desc')
->where('serial', 'LIKE', '%' . $search . '%')
->orWhere('type', 'LIKE', '%' . $search . '%')
->with('warranty')
->whereHas('warranty' , function($query) use ($search) {
   $query->where('first_name', 'LIKE', '%' . $search . '%');
})->get();
使用
whereHas()
进行
查询:

Machine::orderBy('created_at', 'desc')
->where('serial', 'LIKE', '%' . $search . '%')
->orWhere('type', 'LIKE', '%' . $search . '%')
->with('warranty')
->whereHas('warranty' , function($query) use ($search) {
   $query->where('first_name', 'LIKE', '%' . $search . '%');
})->get();

您的查询似乎正常,是否有任何错误??您的查询似乎正常,是否有任何错误??