How to filter a pivot table using Eloquent?
Laravel 4.1 brings native wherePivot and orWherePivot methods, which is directly a solution to my problem.
Laravel 4.1 brings native wherePivot and orWherePivot methods, which is directly a solution to my problem.
You can. From this example in Docs (4.2, 5.0): $user->roles()->sync(array(1 => array(‘expires’ => true))); Hardcoded version for the first two rows: $food = Food::find(1); $food->allergies()->sync([1 => [‘severity’ => 3], 4 => [‘severity’ => 1]]); Dynamically, with your arrays $allergy_ids and $severities in a compatible state (size and sort), you shall prepare your sync data before. … Read more
The use keyword is what you need: $id = 3; DB::transaction(function($id) use ($id) { DB::table(‘users’)->where(‘id’, ‘=’, $id)->get(); }); For PHP 7 (untested, edited as requested by the answer below): $id = 3; DB::transaction(function() use ($id) { DB::table(‘users’)->where(‘id’, ‘=’, $id)->get(); });
You can treat the relationship objects kind of like queries, in that you can call query building functions with them. The example below should get you going in the right direction. class Cars extends Eloquent { function photos() { return $this->hasMany(‘Photo’)->where(‘photos.type’, ‘=’, ‘Cars’); } }
You were very close to the answer $challenge = $this->challenges() ->where(‘open’, true) ->where(function($q) use ($user_id, $opponent_id) { $q->where(function($query) use ($opponent_id, $user_id){ $query->where(‘player_1’, $user_id) ->where(‘player_2’, $opponent_id); }) ->orWhere(function($query) use ($opponent_id, $user_id) { $query->where(‘player_1’, $opponent_id) ->where(‘player_2’, $user_id); }); }) ->first(); Here are the differences between two codes