laravel - One To Many, Many To Many -
$trell = trell::find($trell_id); $builder = $trell->builders(); $codes = $builder->codes(); //undefined method codes...
so undefined method on codes, relationships following: why doesn't work?
trell has 1 defined in model:
public function builders() { return $this->hasone('appsales\\models\\builders'); }
builder has codes defined:
public function codes() { return $this->hasmany('appsales\\models\\codes'); }
codes has:
public function builders() { return $this->belongsto('appsales\\models\\builders'); } $trell->with('builders.codes')->get()->toarray(); works, want "one codes" filtering (where in sql) is.
it doesn't work because call relationship-function doesn't return model relationship.
<?php $trell = trell::find($trell_id); $builder = $trell->builders(); /** * $builder of type \illuminate\database\eloquent\relations\hasone * not have method codes() * don't want relationship, want property model. * call property , not function */ $buildermodel = $trell->builders; $codes = $buildermodel->codes; /** * $codes contains codes collection of models. if want * filter them, relationship / query object , use * regular query builder functions: */ $codes = $buildermodels->codes()->where('key', $value)->get();
you can load trell model:
$trells = trell::with([ 'builders.codes' => function($query) { $query->where('key1', $value1); }, ])->get();
Comments
Post a Comment