samedi 4 août 2018

Laravel 5 - passing variable to relationship model

If I have below models:

class User extends Model{
    protected $someDataFromExt = ['taskID0' => 'test', 'taskID1' => 'ting'];
    public function tasks() { return $this->hasMany('Task'); }
}

class Task extends Model{
    protected $appends = ['ext_data'];
    public function user() { return $this->belongsTo('User'); }
    public function getExtDataAttribute(){ return $this->external_data; }
}

I would like, when I do: $tasks = auth()->user()->tasks->all(); I want to pass $user->someDataFromExt (based on task ID) to task model, so I later in my $tasks variable I can access:

foreach($tasks as $task){
    echo $task->ext_data;
}

Which will return data that was given from user model earlier?

Is this possible? how?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire