how to print province name using provinceid in laravel using eager?

Clash Royale CLAN TAG#URR8PPP
how to print province name using provinceid in laravel using eager?
I am developing app using laravel 5.6.
laravel 5.6
In my application I have three tables as provinces, districts, towns and these models have following relationship:
Province
public function districts()
return $this->hasMany(District::class);
District
public function province()
return $this->belongsTo(Province::class);
Town
public function district()
return $this->belongsTo(District::class);
Now I have data grab using eager loder in Controller:
Controller
$town_list = Town::with('district.province')->groupBy('provinceid')->get();
and dropdown selection input in blade file,
blade
<div class="form-group">
<label for="exampleFormControlSelect1">Province</label>
<select name="province_id" id="province_id" class="form-control input dynamic" data-dependent="district_id" >
<option value="">Select Province</option>
@foreach($town_list as $town)
<option value="$town->provinceid">$town->provinceid</option>
@endforeach
</select>
</div>
now I can see provinceid in My selection input but I need print province name in my selection input, my table structure,
provinces
id name
1 alabama
2 prodova
districts
id name province_id
1 x 1
2 y 2
towns
id name district_id province_id
1 gh 1 1
2 ju 2 2
1 Answer
1
Firstly, you can remove that province_id from your Town table. District of the town gives you required province id ($town->district->province_id or $town->district->province->id).
province_id
Town
$town->district->province_id
$town->district->province->id
Also, to print Province name, you can chain your relationships. As mentioned, you can get it using $town->district->province->name.
$town->district->province->name
<option value="$town->district->province->id">$town->district->province->name</option>
->groupBy('provinceid') is this a valid one? there doesn't seem to be any column with name provinceid. Also, can you message me full error? It might be more helpful to detect issue.– Pusparaj
Aug 9 at 14:07
->groupBy('provinceid')
provinceid
full error msg is Trying to get property of non-object (View: C:UsersbandaDesktopdgtrresourcesviewscarscarform.blade.php) in 218536a4e8b355ff34185defd1fcb8465089362a.php line 26
– banda
Aug 9 at 14:16
my line 26 is <option value="$town->provinceid">$town->district->province['provincename']</option>
– banda
Aug 9 at 14:17
<option value="$town->district->province->id">$town->district->province->name</option> or <option value="$town->province_id">$town->district->province->name</option>.– Pusparaj
Aug 9 at 14:34
<option value="$town->district->province->id">$town->district->province->name</option>
<option value="$town->province_id">$town->district->province->name</option>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
occurred this error msg Trying to get property of non-object
– banda
Aug 8 at 15:59