Laravel eloquent add extra models to hasMany -


i have user model , each user has multiple licenses. there 2 default licenses apply users not in licenses table , need created on fly using data contained in user model.

how can create 2 licenses each time i'm getting user's licenses , add output of licenses()?

class user extends model implements authenticatablecontract, canresetpasswordcontract {      use authenticatable, canresetpassword;      protected $table = 'users';     public $timestamps = false;      protected $fillable = ['name', 'email', 'password'];      protected $hidden = ['password', 'remember_token'];      public function licenses()     {         return $this->hasmany('app\license', 'user_id', 'id')->where('deleted', '=', '0');     } } 

you create function in model call licenses , add licenses.

remember test places use function no strange stuff happen.

<?php  class user extends model implements authenticatablecontract, canresetpasswordcontract {      use authenticatable, canresetpassword;      protected $table = 'users';     public $timestamps = false;      protected $fillable = ['name', 'email', 'password'];      protected $hidden = ['password', 'remember_token'];      // hasmany function     public function _licenses()     {         return $this->hasmany('app\license', 'user_id', 'id')->where('deleted', '=', '0');     }      // new licenses function     public function licenses()      {         // licenses database         $licenses = $this->_licenses;          // add  other lisences         $licenses = $licenses->add(new license([ "user_id" => $this->id, "name" => "foo" ]));         $licenses = $licenses->add(new license([ "user_id" => $this->id, "name" => "bar" ]));          // return new collection         return $licenses     } } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -