Skip to content
Advertisement

Laravel return json with eloquent collection relationship values

I have the following function inside the UsersApiController class :- public function getKhatmas()

JavaScript

The above code give the following response :-

JavaScript

Relationship function in User Model file :-

JavaScript

AppKhatma file content :-

JavaScript

In above json response , (“type”: 2) is the foreign key of AppKhatmaType Model . I want instead of json response with the foreign key of KhatmaType Model, i want it to return the column “title” from AppKhatmaType to be like this :-

JavaScript

`I tried with the following :-

JavaScript

But it return error : Method IlluminateDatabaseEloquentCollection::with does not exist

Advertisement

Answer

Fixed by using the following query builder:-

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement