Code/PHP

XorY methods

kolbe_starziki 2021. 3. 4. 08:54

엘로퀀트는 2가지 기능응 합친 메소드가 있따. 이런 메서드는 X하고 안되면 Y해라.

 

Example 1  findOrFail():

Instead of:

$user = User::find($id); if (!$user) { abort (404); }

Do this:

$user = User::findOrFail($id);

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

 

 

Example 2  firstOrCreate():

Instead of:

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

Do just this:

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

'Code > PHP' 카테고리의 다른 글

여러 권한을 한번에 체크하자.  (0) 2021.03.08
DD()는 종료합니다.  (0) 2021.03.05
Model all: columns  (0) 2021.03.03
Request->All 보단 Request->expect, Request only 를 쓰자,.  (0) 2021.03.01
Laravel Model의 boot()  (0) 2021.02.28