laravel - Querying Relations -
laravel - Querying Relations -
i got these tables:
table: investigation id name start_city end_city table: city id name
start_city
, end_city
in table investigation
refers city.id
.
this investigation
model:
public function start_city(){ homecoming $this->hasone('city', 'id', 'start_city'); } public function end_city(){ homecoming $this->hasone('city', 'id', 'end_city'); }
city
model:
public function start_city(){ homecoming $this->belongsto('investigation', 'id', 'start_city'); } public function end_city(){ homecoming $this->belongsto('investigation', 'id', 'end_city'); }
investigation
controller:
public function show($id){ echo '<pre>'; var_dump(investigation::find($id)->start_city->name); }
i trying property of non-object
. suspect relationship somehow 'broken'. how prepare this? tried switch hasone
, belongsto
doesn't alter anything.
ps: please don't comment on code convention since 'translated' code other language doesn't have plural form.
rename relations camelcase
:
public function startcity(){ homecoming $this->hasone('city', 'id', 'start_city'); } public function endcity(){ homecoming $this->hasone('city', 'id', 'end_city'); }
and same other models.
then can utilize dynamic properties
below:
$investigation->startcity;
otherwise can't utilize dynamic properties , need this:
$investigation->start_city()->getresults()->name;
laravel eloquent
Comments
Post a Comment