Eloquent ->first() if ->exists()

前端 未结 5 605
天涯浪人
天涯浪人 2020-12-23 09:08

I want to get the first row in table where condition matches:

User::where(\'mobile\', Input::get(\'mobile\'))->first()

It works well, bu

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 09:43

    An answer has already been accepted, but in these situations, a more elegant solution in my opinion would be to use error handling.

        try {
            $user = User::where('mobile', Input::get('mobile'))->first();
        } catch (ErrorException $e) {
            // Do stuff here that you need to do if it doesn't exist.
            return View::make('some.view')->with('msg', $e->getMessage());
        }
    

提交回复
热议问题