Jordy Groote Jordy Groote - 4 months ago 22
MySQL Question

Need help building query laravel 5.2

I'm trying to get rings from the database. but only filter is the homepage is 1 or 0.

I only need the rows where homepage is 1.

enter image description here

This is what i tried

$ringen = RingKoppelCategory::with('ringen')->get()->where('homepage', '=' , 1);


returns null

And when i put the ->get() at the end of the query builder it checks the ringkoppelcategory table for a homepage which is not what i want it to do.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'homepage' in 'where clause' (SQL: select * from `ringkoppelcategory` where `homepage` = 1


I need to get the rings relationship from the ringkoppelcategory, but only the rings where the homepage is 1.

Answer

You need to use a function to pass along a where within your with.

$ringen = RingKoppelCategory::with(['ringen' => function ($query) {
    $query->where('homepage', '=' , 1);
}])->get();

More information is found in the documentation

Comments