Support the ongoing development of Laravel.io →
Database Eloquent

I have 3 tables: Users, Tasks and Records.

Users have many tasks Tasks have many records.

I'm trying to select all records in a given date for a specific user. (these 2 tables "join" indirectly through the tasks table)

The two separate selects are these:

$records = Record::where('date','=', $date ) $tasks = Task::where('user_id', '=', $user_id)

I'm trying something like this:

$lastRecord = $user->tasks->records->get('date');

Obviously doesn't work, but kinda explains what I'm trying to retrieve.

The example sql query would be something like this:

select * from records join tasks where records.task_id == tasks.id join users where tasks.user_id == users.id where records.date == '2014-07-13'

Can someone help me out? I'm new to eloquent.

Last updated 3 years ago.
0

Have a look at this relationship

http://laravel.com/docs/eloquent#has-many-through

I believe it is exactly what you need

Last updated 3 years ago.
0

Wow, so simple and effective, Laravel is awesome Thanks TorchSK

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

raphadko raphadko Joined 13 Jul 2014

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.