mercredi 29 mars 2017

Use one polymorphic relation in many Models

I have a polymorphic relation on my Laravel 5.4 User Model:

> Relation::morphMap([
>     'employee'=>'Modules\Employees\Entities\Employee',
>     'partner'=>'Modules\Partners\Entities\Partner',
> 
> ]); 
class User extends Authenticatable {
>     public function userable(){
>         return $this->morphTo();
>     } }

users,employees and partners are DB, in users i have users.userable_id users.userable_type , wich containts id from employees or partners, depend what i have on userable_type i retrive data with App\User::with('userable')->get() and view data in Datatbles JS with "data.userable.name" (where name is column from employees or partners DB)

Now... i have anathore model House, in this i have next function

public function createdbyuser()
{
    return $this->belongsTo('App\User', 'createdbyuser_id', 'id');
}

but i can get only data from User(username, pass,userable_id, userable_type), i need to go one step forward to name for this user which are stored in Employes or Partner, depend what is set in users.userable_type

How to get name from Employees or Partnert with userable function fromUser model in House Model/House Controller.

Thanks in advance!



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

Aucun commentaire:

Enregistrer un commentaire