php - Eloquent ORM not fetching data using relationship laravel -
i new laravel , using eloquent orm. issue trying records using relationships with() function. issue eloquent generates , applies right query not returns result. if test same generated query on mysql works fine.
following 2 tables involved in this:
properties: id , name , locality_id
localities: id, name , type , adjoining
now relationship between above mentioned tables 1 many relationship.
property model:
protected $table = 'properties'; protected $guarded = array('id'); public function localityareaandcity() { return $this->belongsto('locality','locality_id') ->leftjoin('localities ls', function($join) { $join->on('localities.id', '=', 'ls.adjoining') ->where('localities.type', '=','area'); }); ->select(array('localities.name localityprimaryname', 'localities.type localityprimarytype', 'ls.name localitysecondaryname', 'ls.type localitysecondarytype')); }
locality model:
public $timestamps = false; protected $table = 'localities'; protected $guarded = array('id'); public function properties() { return $this->hasmany('property'); }
eloquent query:
$properties = property::with('localityareaandcity')->get();
db::getquerylog() result:
select `localities`.`name` `localityprimaryname`, `localities`.`type` `localityprimarytype`, `ls`.`name` `localitysecondaryname`, `ls`.`type` `localitysecondarytype` `localities` left join `localities` `ls` on `localities`.`id` = `ls`.`adjoining` , `localities`.`type` = ? `localities`.`id` in (?, ?, ?, ?, ?)
know if use above mentioned query in mysql returns data using eloquent orm returns null. please help..
just added locality_id select , worked. eloquent works have 2 arrays related items , want match them - if there's no foreign key in 1 of them, points primary key in other one, can't that. eloquent same.
you must select both keys of relation (most pk on 1 model , fk on other one).
Comments
Post a Comment