简体   繁体   中英

How to compare on Laravel pivot amounts with table column

I am really stuck with this.

I have a Many to Many relationship with Actors and Works. I need to get all the works that don't have all the actors they need. The actors needed per work is a property on the Works table called "extras_needed"

So far I've tried withCount, but you can't use it on a where. This is the closest thing I have which is:

$works = $works->has('actors','<','extras_needed');

But its trying to use extras_needed as a string and I need it to treat it as the column per se.

Any advice? Thanks in advance

You'll want to use DB::raw :

Sometimes you may need to use a raw expression in a query.

Armed with this, we can turn your query into:

$works = $works->has('actors', '<', \DB::raw('extras_needed'));

Hope this helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM