将Custom Get Attribute字段检索到collection. 我现在需要在使用前过滤get()
例子:
对于is_paid自定义属性
//is_paid
public function getIsPurchasedAttribute()
{
//Since purchased has one relation with user
return !empty($this->purchasedRelation) ? true : false;
}
我现在需要User基于 is_purchased 进行检索,例如:
public function index(Request $request){
$isPaid = $request->is_paid;
$users = User::query();
if($isPaid){
$users = $users->where('is_paid', true);
}
$users = $users->paginate(10);
return view('user.index', [
'users' => $users
]);
}
如果我这样做,它将返回:
未找到列:1054 'where 子句中的未知列'is_paid'
是的,我可以这样做,但我现在无法使用paginate功能:
$users = User::get();
$users = $users->where('is_paid', 1);
return view('user.index', [
'users' => $users
]);
喵喔喔
倚天杖