Laravel : Handle findOrFail( ) on Fail

后端 未结 6 1722
灰色年华
灰色年华 2021-02-13 17:31

I am looking for something which can be like findOrDo(). Like do this when data not found. Something could be like

Model::findOrDo($id,function(){
   return \"Da         


        
6条回答
  •  礼貌的吻别
    2021-02-13 18:12

    use Illuminate\Database\Eloquent\ModelNotFoundException;
    
    // Will return a ModelNotFoundException if no user with that id
    try
    {
        $user = User::findOrFail($id);
    }
    // catch(Exception $e) catch any exception
    catch(ModelNotFoundException $e)
    {
        dd(get_class_methods($e)); // lists all available methods for exception object
        dd($e);
    }
    

提交回复
热议问题