Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

The solution I came up with is this:

$tableA = TableA::with('tableB')
        ->join('tableB', 'tableB.id', '=', 'tableA.tableB_id')
        ->where('tableA.fieldA', '=', valueA)
        ->where('tableB.fieldB', '=', valueB)
        ->orderBy('created_at', 'DESC')->paginate(10);

That join of course does affect query performance though.

Last updated 2 years ago.
0

Assuming you've setup one of the Eloquent relations you can use the following. http://laravel.com/docs/eloquent#querying-relations

TableA::with('tableB')
    ->where('fieldA', $valueA)
    ->whereHas('tableB', function($query)
{
    // Now querying on tableB
    $query->where('fieldB', $valueB);
})->orderBy('created_at', 'DESC')
    ->paginate(10);
Last updated 2 years ago.
0

Oh beautiful, that's exactly what I was looking for, thanks!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Meowts meowts Joined 10 Jun 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.

© 2024 Laravel.io - All rights reserved.