Laravel : Handle findOrFail( ) on Fail

后端 未结 6 1742
灰色年华
灰色年华 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:35

    as of Laravel v5.7, you can do this (the retrieving single model variation of @thewizardguy answer)

    // $model will be null if not found
    $model = Model::where('id', $id)->first();
    
    if($model) {
        doActions();
    }
    

提交回复
热议问题