1

I have many to many relationship. In postman I pass find_id and status.

My query is like: UPDATE finds_user SET status = TRUE WHERE find_id = 2 AND user_id = 1.

I do it now like that:

DB::table('finds_user')
    ->where('find_id', '=', $request->find_id)
    ->where('user_id', '=', $user->id)
    ->update(['status' => $request->status]);

How I can do it with Eloquent instead of DB Query Builder?

Thank you a lot!

JohnSmith
  • 141
  • 3
  • 12
  • Does this answer your question? [How to update a pivot table using Eloquent in laravel 5](https://stackoverflow.com/questions/33543897/how-to-update-a-pivot-table-using-eloquent-in-laravel-5) – Oleg Nurutdinov Apr 18 '20 at 16:53
  • No. I just want convert db query to eloquent. – JohnSmith Apr 18 '20 at 17:04

1 Answers1

1

we have tow condition for update:

1- find_id = 2

2- user_id = 1.

suppose the relation name in the user model called 'finds' the query will look like this:

User::find(1)->finds()->newPivotQuery()->where('find_id',2)->update(['status'=>true])

and if you have a model for the pivot table with name like "FindUser":

FindUser::where('user_id',$user->id)->where('find_id',2)->update(['status'=>true]);
OMR
  • 11,736
  • 5
  • 20
  • 35
  • yes, it is, the method newPivotQuery() make the query directly for the pivot table, i have tested it – OMR Apr 18 '20 at 17:24
  • you could do: $user->finds()->newPivotQuery()->where('find_id',2)->update(['status'=>true]); will give the same result – OMR Apr 18 '20 at 17:25
  • what is the name of the many to many relation between user and find models? – OMR Apr 18 '20 at 17:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211961/discussion-between-omr-and-johnsmith). – OMR Apr 18 '20 at 17:33