In laravel with Model we can get all Table details
Locations::all();
But I want to get with Foreign key data with the all data.
like there is
id , name , location_id
so it has to get
location_id name
So i get it like
id , name, location_id with location_id_Name
Is it possible to get that result by using laravel basic model. Or I have to change some.
There is answer How to retrieve full data of record by its foreign key in laravel? But then I have to create a new function to add more component of each array. Which I don't want as I suspect there should be better way.
Solution which i found
Location::with('locationid')->get();
where loctionid declared in the model Location
public function locationid(){
return $this->belongsTo(Location::class, 'location_id');
}
So now location load with parent. And also it is very much efficient. If anyone know what will better solution please just comment or answer it. Best answer on stackoverflow look like https://stackoverflow.com/a/66187247/12657567