Hello, I have a table communications and a table agents. Each communication belong to an agent. I would like to return all the columns in Communications and another property in the store "agent_name" that will return the name column in the agent table. The columns that link communications and agents are: communications.agent_id=agents.id How can i return the json that will return also agent_name as a property and not in an array "agent": { "name": "אירית אלפסי", "id": 2 } Cause when using "with"function it returns the json as an array and not as a property.
Here is the json that i get when using "with" function(Communication::with('agent')->get();)
{ "success": "true", "data": [ { "id": 1, "agent_id": 2, "agent": { "name": "Jessy", "id": 2 } ], "total": 1 }
Here is the json that i would like to get:
{ "success": "true", "data": [ { "id": 1, "agent_id": 2, "agent_name": "Jessy", ], "total": 1 }
Please help me i'm new in laravel Thanks
You would need to manually build the data up into a PHP array then use Response::json($array)
to serve that as json.
I could be wrong but Laravel will always serve the agent in an array because its a relation to the communication model, rather than part of the communication model.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community