Support the ongoing development of Laravel.io →
Database Eloquent

I'm wondering how to get the correct id on join without specifying the table or 'as' in the select part of the query.

I have two models, Tickets and Users:

Tickets.php has (pseudo-code): public function getUrl() { return 'ticket/{ $this->id }'; }

If I do: Tickets::join('Users'), mysql returns a table with two id's, so Ticket::getUrl() returns

ticket/{ users.id }

instead of:

ticket/{ tickets.id }

I'm wondering if there's a way to cajigger the model to make sure it returns $this->ticket.id and not any other id.

Last updated 2 years ago.
0

use a select statement to take only the fields you need.

Tickets:select('tickets_table.*, users_table.name as user_name')->join('users_table', 'tickets_table.owner_id', '=', 'users_table.id');

currently there's no way of doing this automatically .

Last updated 8 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.