Laravel 预加载和 LIKE 过滤器
Laravel eager loading and LIKE filter
我正在处理一个典型的帐户-> 邮政编码场景。我有 2 个模型 2:帐户和邮政编码,在我添加的帐户中
public function zipcode() {
return $this->belongsTo(Zipcode::class);
}
index.blade.php是这样的:
<td>{{$account->id}}</td>
<td>{{$account->fullname}}</td>
<td>{{$account->address}}</td>
<td>{{$account->zipcode->fulllocation}}</td>
...
我为 accounts.fullname 中包含的字符串添加了一个过滤器(列表顶部的基本输入文本),所以控制器是这样的:
public function index(Request $request)
{
$search = $request->search;
//The instructions in the closure given to when() will only be applied if the first argument is evaluated to true
$accounts = Account::with('zipcode')//https://laravel.com/docs/4.2/eloquent#eager-loading
->when($search,function ($query) use ($search) {
$query->where('fullname','like','%'.$search.'%');
return $query;
})
->latest()
->paginate(15);
return view('accounts.index', compact('accounts'))->with('search',$search);
}
此时场景已经过测试并且可以正常工作,如果没有插入搜索,则会列出所有帐户,否则结果会被正确过滤。
我的需求是在accounts.fullname或accounts.zipcode.fulllocation中搜索插入的字符串。我尝试以这种方式修改 controller:
$accounts = Account::with('zipcode')//https://laravel.com/docs/4.2/eloquent#eager-loading
->when($search,function ($query) use ($search) {
$query->where('fullname','like','%'.$search.'%');
$query->orWhere('fulllocation','like','%'.$search.'%');//ADDED THIS
return $query;
})
->latest()
->paginate(15);
当我尝试时出现了这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fulllocation' in 'where clause' (SQL: select count(*) as aggregate from `accounts` where `fullname` like %foo% or `fulllocation` like %foo%)
感谢任何形式的帮助。
您收到该错误的原因是查询在帐户 table 中查找 fulllocation
,而实际上它在 zipcodes
table 中。
您可以尝试类似的方法:
$query->where('fullname', 'like', '%' . $search . '%')
->orWhereHas('zipcode', function ($q) use ($search) {
$q->where('fulllocation', 'like', '%' . $search . '%');
});
https://laravel.com/docs/5.3/eloquent-relationships#querying-relationship-existence
希望对您有所帮助!
我正在处理一个典型的帐户-> 邮政编码场景。我有 2 个模型 2:帐户和邮政编码,在我添加的帐户中
public function zipcode() {
return $this->belongsTo(Zipcode::class);
}
index.blade.php是这样的:
<td>{{$account->id}}</td>
<td>{{$account->fullname}}</td>
<td>{{$account->address}}</td>
<td>{{$account->zipcode->fulllocation}}</td>
...
我为 accounts.fullname 中包含的字符串添加了一个过滤器(列表顶部的基本输入文本),所以控制器是这样的:
public function index(Request $request)
{
$search = $request->search;
//The instructions in the closure given to when() will only be applied if the first argument is evaluated to true
$accounts = Account::with('zipcode')//https://laravel.com/docs/4.2/eloquent#eager-loading
->when($search,function ($query) use ($search) {
$query->where('fullname','like','%'.$search.'%');
return $query;
})
->latest()
->paginate(15);
return view('accounts.index', compact('accounts'))->with('search',$search);
}
此时场景已经过测试并且可以正常工作,如果没有插入搜索,则会列出所有帐户,否则结果会被正确过滤。
我的需求是在accounts.fullname或accounts.zipcode.fulllocation中搜索插入的字符串。我尝试以这种方式修改 controller:
$accounts = Account::with('zipcode')//https://laravel.com/docs/4.2/eloquent#eager-loading
->when($search,function ($query) use ($search) {
$query->where('fullname','like','%'.$search.'%');
$query->orWhere('fulllocation','like','%'.$search.'%');//ADDED THIS
return $query;
})
->latest()
->paginate(15);
当我尝试时出现了这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fulllocation' in 'where clause' (SQL: select count(*) as aggregate from `accounts` where `fullname` like %foo% or `fulllocation` like %foo%)
感谢任何形式的帮助。
您收到该错误的原因是查询在帐户 table 中查找 fulllocation
,而实际上它在 zipcodes
table 中。
您可以尝试类似的方法:
$query->where('fullname', 'like', '%' . $search . '%')
->orWhereHas('zipcode', function ($q) use ($search) {
$q->where('fulllocation', 'like', '%' . $search . '%');
});
https://laravel.com/docs/5.3/eloquent-relationships#querying-relationship-existence
希望对您有所帮助!