dimanche 5 février 2017

How to get a single record from One to Many polymorphic relation in Laravel 5.4

I have two model with One to Many polymorphic relation like:

class Address extends Authenticatable
{
    public function addressable()
    {
        return $this->morphTo();
    }
}


class User extends Authenticatable
{
    public function addresses()
    {
        return $this->morphMany('App\Address', 'addressable');
    }
}

In address model there is a column named is_primary and one user can have only one primary address. Now in user update form I want to get the primary address only from the address model.

I know I can pull all addresses and check which one is the primary address or this:

public function primaryAddress()
{
    return $this->addresses()->where('is_primary_address',true)->first();
}

But the problem is the form field doesn't get the value and I want to access the primary address like $user->address_line_1

How can i achive this goal?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kGonoz
via IFTTT

Aucun commentaire:

Enregistrer un commentaire