laravel eloquent hasmany select 1

laravel eloquent hasmany select

$data = \App\User::with('articles:id,title,user_id')->get();
// user_id is important in hasmay relationships

Here is what the above code is Doing:
1. We are getting all the users from the database.
2. We are eager loading the articles relationship.
3. We are only selecting the id, title, and user_id columns from the articles table.
4. We are getting the results and storing them in the $data variable.

Similar Posts