I am trying to get two related objects in Laravel using the download as per the documentation.
https://laravel.com/docs/5.4/eloquent-relationships#eager-loading
My models:
class Lead extends Model { public function session() { return $this->hasOne('App\LeadSession'); } } class LeadSession extends Model { public function lead() { return $this->belongsTo('App\Lead'); } }
I want to get both objects with one SQL query. Basically I want to execute:
select * from lead_sessions as s inner join lead as l on l.id = s.lead_id where s.token = '$token';
and then have access to the LeadSession and Lead objects. Here is the php code I'm trying to do:
$lead = Lead::with(['session' => function ($q) use ($token) { $q->where('token','=',$token); }])->firstOrFail(); print($lead->session->id);
I also tried:
$lead = Lead::whereHas('session', function($q) use ($token) { $q->where('token','=',$token); })->firstOrFail(); print($lead->session->id);
and
$session = LeadSession::with('lead')->where('token',$token)->firstOrFail(); print($session->lead->id);
In all three cases, I run two queries: one for the leads
table, and the other for the lead_sessions
table.
Is this possible in Oratory? In my opinion, this should be a standard ORM operation, but for some reason I have been struggling with it all day.
I do not want to use Query Builder, because after that I want to use Eloquent objects and their functions.
I come from Python and Django, and I want to replicate the behavior of the select_related
function in Django.