0

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

Puneet Sharma
  • 73
  • 2
  • 9

1 Answers1

2

See the official documentation of Laravel framework. You may need Eager Loading. https://laravel.com/docs/8.x/eloquent-relationships#eager-loading

cednore
  • 874
  • 4
  • 20
  • Thanks for you i get what i want. have some issue but https://stackoverflow.com/questions/66185760/attempt-to-read-property-name-on-null solve problem which i have so everything fine – Puneet Sharma Jun 28 '21 at 21:01