In order you to do that you need to make the inverse relationship:
class Preference extends Model
{
return $this->belongsTo('App\User');
}
For further clarification see: https://laravel.com/docs/5.8/eloquent-relationships#one-to-many
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have these three tables.
Accounts
Preferences
Data example,
AccountPreferences
I want to access all account preferences of a user.
class Account extends Model
{
public function preferences()
{
return $this->hasMany('App\AccountPreference', 'account_id', 'id'); //model, foreign_key, local_key
}
}
class Preference extends Model
{
// just default empty model class
}
class AccountPreference extends Model
{
// just default empty model class
}
When I do
$account = Account::find(134);
$pref = $account->preferences()->get();
dd($pref);
When I do $pref = $account->preferences()->get(); I want to retrieve preference name from preferences table too.
How do I do that?
Thanks
Please or to participate in this conversation.