1 year ago

Laravel Updating Relationship Column in WhereHas

Posted 1 year ago by undrftd_

I wanted to update a column in my model with a wherehas. However, I'm not sure how to do this. Here's my query below

$unused = Sale::whereHas('salesdetails',function($query) {
             $query->where('product_id', '<=', 24)->where('switch', 1);
        })->where(DB::raw("(DATE_FORMAT(transaction_date,'%Y-%m-%d'))"), '<', $now)->update(['switch' => 0]); 

In the query above, I want to update all the switches to '0' from the salesdetails relationship. How can I do this one?

Please sign in or create an account to participate in this conversation.