拉维尔5号*
我的Case模型与Customer模型有一对一的关系,定义如下:

public function customer
{
   return $this->hasOne(Customer::class, 'id', 'customer');
}

在cases列表(cases.index)中,它根据关系显示客户名称。
我正在尝试创建搜索,并根据客户名称列出所有案例。
有没有基于这种关系的本地搜索方法?

最佳答案

使用whereHas()方法。例如:

Case::whereHas('customer', function($q) use($name) {
    $q->where('name', 'like', '%' . $name . '%');
})->get();

关于php - 在Laravel中基于外键进行搜索,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48928160/

10-09 23:02
查看更多