Support the ongoing development of Laravel.io →
Eloquent

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

Last updated 3 years ago.
0

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.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.