子查詢寫法laravel
$stores = Stores::select('id','name')->whereNotIn('id', function ($query) use($id){ $query->select('stores_id')->from('fook_article_stores')->where('fook_article_stores.article_id', $id); })->get();
Stores::where('verification_pass',$verification_pass)->where('id','<>',$id)->where('business_id', function ($query) use($id){ $query->select('business_id')->from('fook_stores')->where('fook_stores.id', $id); })->get();
Relation 表結構spa
要求:篩選出沒有與本用戶創建關係的用戶code
高效寫法blog
$users = User::whereNotIn('id', function ($query) use($user){ $query->select('toUserId')->from('relation')->where('relation.userId', $user->id); });
$actionRelationsArray = Relation::select('toUserId')->where('userId', $user->id)->get()->pluck('toUserId'); $users = User::whereNotIn('id', $actionRelationsArray);