Laravel : Handle findOrFail( ) on Fail

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

    An alternative process could be to evaluate a collection instead. So,

    $modelCollection = Model::where('id', $id)->get();
    if(!$modelCollection->isEmpty()) {
        doActions();
    }
    

    I agree it isn't as elegant, a one-liner or as case specific as you or I might like, but aside from writing a try catch statement every time, it's a nice alternative.

提交回复
热议问题