Hello Devs, 

In this tutorial, we will learn Laravel 7/6 find() and findOrFail() Eloquent Query Example

findOrFail method is used to get a record and not found record than show automatically 404 Page

Follow this step by step guide below. 


Example Find Method

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index(){
    
  $userId = 10;
  $user  = User::find($userId);

  if(is_null($user)){
  	return abort(404);
  }

  dd($user);
}


Example findOrFail Method

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index(){
    
  $userId = 10;

  $user  = User::findOrFail($userId);
  
  dd($user);
}


May this example help you.