// This line fetch the datas. There's no need for select and get.
$requested_station = RequestedStation::with(['localbody'])->get();
Jan 1, 2018
2
Level 2
How to join two tables using Eloquent ?
Dear Friends,
I have 2 tables need to join .
- 'local_bodies' with structure
lbid integer NOT NULL, lbname_mal character varying(50)
- 'requested_stations' with structure
id integer NOT NULL DEFAULT nextval('ulbgt_requested_stations_id_seq'::regclass), requested_station integer NOT NULL, option_number smallint NOT NULL
Here these two tables need to join . One person can 'request' 3 requested stations with 'option number 1,2,3'.
I have relation model for 'local_bodies' as
public function requestedstation()
{
return $this->hasMany('App\Ulbgt\LocalBody','lbid');
}
for 'requested_stations' as
public function localbody()
{
return $this->belongsTo('App\Ulbgt\LocalBody','id');
}
I wrote query using eloquent as
$requested_station = RequestedStation::with(['localbody' => function ($qry) {
$qry->select('lbid','lbname_mal');
}])->select('id','requested_station','option_number')->get();
but got wrong result for 'lbname_mal'.
Please advise ... I suspect some problem in query or relation model
Waiting fast reply
Thanks
Please or to participate in this conversation.