laravel - Eloquent MorphMany relationship proxy attribute -


in application made strange observation, seemingly equal operation has different results another.

i have following tables:

aliases:

$table->engine = "innodb"; $table->increments("id"); $table->morphs("aliasable"); $table->string("alias"); $table->string("locale"); 

categories:

$table->engine = "innodb"; $table->increments('id'); 

the category model has relationship alias model:

public function aliases() {     return $this->morphmany("alias", "aliasable"); } 

when trying manipulate data of alias of category there difference between 2 following methods:

$category = category::find(1); $alias = $category->aliases()->first(); $alias->alias = "test"; $alias->save();  $category = category::find(1); $alias = $category->aliases()->first()->alias = "test"; $category->aliases()->first()->save(); 

the first 1 working, second 1 not saving change. second version working try implement proxy attribute on category model in order change alias so:

$category = category::find(1); $category->germanalias = "heidi"; $category->push(); 

do have idea why way not working expected?

the key thing understand here way relationships cached on model. when access relationship method directly, no caching occurs. instantiating new query based off of relationships definition.

$query = $category->aliases(); $alias = $query->first(); 

if access relationship if member/attribute of model, load , cache relationship collection on model.

$collection = $category->aliases; $alias = $collection->first(); 

future attempts access related model method reference same cached collection of models. should work.

$category->aliases->first()->alias = 'test'; $category->aliases->first()->save(); 

within /illuminate/database/eloquent/model __get() magic method redirects request undefined members getattribute() method. code comments pretty job of explaining rest checks for, loads, caches , reuses relationships.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -