$books = User::with('achievements:id,name,achievement_id')->get();
Do note that you have to include the "id", "foreign key" and any necessary columns as this is one of the required for this type of eager load.
Relationship Definition
The relationship is defined like usual.
<?php namespace App\Models; class User extends Model { public function achievements() { return $this->hasMany(Achievement::class); } }
Inverse Relationship
For the inverse relationship, it's defined like below.
<?php namespace App\Models; class Achievement extends Model { public function user() { return $this->belongsTo(User::class); } }
Leave a reply