Yes, you can eager load pivot relations in Laravel without installing a package. You can use the withPivot() method in your model to specify the columns you want to retrieve from the pivot table. Here's an example:
class User extends Model
{
public function roles()
{
return $this->belongsToMany(Role::class)->withPivot('created_at', 'updated_at');
}
}
In this example, we're eager loading the created_at and updated_at columns from the pivot table for the roles relationship.
Then, you can access the pivot data using the pivot attribute on the related model. For example:
$user = User::with('roles')->find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
echo $role->pivot->updated_at;
}
This will output the created_at and updated_at values for each role that the user has.