XorY methods
XorY methods
Eloquent has quite a few functions that combine two methods, like “please do X, otherwise do Y”.

firstOrCreate()

Instead of:

$user = User::where('email', $email)->first();if (!$user) { User::create([ 'email' => $email ]);}

Do just this:

$user = User::firstOrCreate(['email' => $email]);

What's your reaction?

Comments

https://magazine.tdbcomputing.com/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!

Facebook Conversations

Disqus Conversations