Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to access relationship attributes as own in laravel model?

I have 2 tables: users and users_details

-- users has 3 columns [id,username,password]

-- users_details has [id,user_id,name,address]

I have models for each of them with a relation from users to users_details.

    public function details()
    {
        return $this->hasOne(UserDetails::class, 'user_id', 'id');
    }

my question comes here: Is there a way to access UsersDetails attributes as User only.

e.g: {{ $user->name }} : {{ $user->address }} instead of {{ $user->details->name }} : {{ $user->details->address }}

Keep in mind that these tables are only examples and don't have their real range of columns. I need this to implement Laravel/Cashier but this plugin needs 4 new columns in [users] table and I want to put them in [users_details] table.

I haven't tried anything because I don't really know if it is possible. Except for the moment the only workaround that I know of is to make getNameAttribute method in User:

    public function getNameAttribute()
    {
        return $this->details->name;
    }
like image 969
Griminvain Avatar asked Mar 04 '23 18:03

Griminvain


2 Answers

I'm not sure I would recommend it as it will just add magic to how things work and might be hard to understand 6 months from now.

But you could do something like that in your User model.

Edited

public function getAttribute($key)
{
    // If attribute exists on the user, return that
    return parent::getAttribute($key)
        // otherwise fallback to details attribute
        ?? optional($this->details)->getAttribute($key);
}
like image 199
DevK Avatar answered Mar 22 '23 23:03

DevK


yes. you can do it in controller before pass $user to your view. like this.

$user->load('details');

then you pass the $user to your view like this:

return view('your_view' , compact('user'));

then in your view use the following code:

{{ $user->details_table_field }}

you can search about eager loading : https://laravel.com/docs/5.7/eloquent-relationships#eager-loading

like image 22
Reza sh Avatar answered Mar 22 '23 23:03

Reza sh