Laravel attach pivot to table with multiple values

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

Nested ‘AND OR’ Query in Eloquent

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