is it possible to use WhereIn / WhereNull or Condition on firstOrNew, firstOrCreate, updateOrCreate?

  eloquent, laravel

im trying to understanding about laravel and shorten my code

firstOrNew, firstOrCreate, updateOrCreate
from what ive been reading you can do simple condition like :

example 1

condition where email matched then updateOrCreate:

$user = User::updateOrCreate(
    ['email' =>  request('email')],
    ['name' => request('name')]
);

example 2 – this is what ive been usually code my update / create

but is it possible to use whereNull, whereIn or other conditions on updateOrCreate like this example 2 ?

$user = User::whereNull( 'email' )->first();

$vars =
[
'email' => request('email'),
'name' => request('name')
];

if($user){
    $user->update($vars);
}else{
    $user = User::create($vars);
}

is example 2 the best approach or shorter code for update and creating entry in database ?

thanks if anyone willing to help

Source: Laravel

Leave a Reply