'Laravel boolean validation based on others column

I want to validate the is_default column based on domain_id. The condition is for every domain_id there can be only one(single) 1; others multiple will be 0 (zero).

I try to make a rule in Laravel that's like the following. But It's not working; I know it's wrong. So what would be the best query?

$query = Restaurant::where($attribute, $value)
    ->where('domain_user.domain_id', request()->get('domain_id'));

enter image description here



Solution 1:[1]

Here is something you might want:

function updateDefault($domain_id, $name) {
    //updating default domain with given name
    Restoraunt::where('domain_id',$domain_id)->where('name', $name)->first()->update(['is_default' => 1]);
  
    //setting all other models with same domain_id to 0
    Restoraunt::where('domain_id', $domain_id)->where('name', '!=', $name)->update(['is_default' => 0]);
  
}

enter image description here

If you for example use updateDefault(1, 'ABC - 2') results would be : enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aleksandar ?oki?