Category Archives: 开发技术

Laravel中Model为空的几种判断

  1. isEmpty判断
    $user = User::where('mobile', Input::get('mobile'))->get();
    
    if (!$user->isEmpty()){
        $firstUser = $user->first()
    }
  2. Exception的处理
    try {
        $user = User::where('mobile', Input::get('mobile'))->firstOrFail();
        //Do stuff when user exists.
    } catch (ErrorException $e) {
        //Do stuff if it doesn't exist.
    }
  3. 其他
    //Use either one of the below. 
    $users = User::where('mobile', Input::get('mobile'))->get(); //Collection
    $users = User::where('mobile', Input::get('mobile'))->first(); //Model or null
    
    if (count($users)){
        //Use the collection, to get the first item use $users->first().
        //Use the model if you used ->first();
    }

使用PHPSTORM和XDEBUG优化PHP程序

Xdebug将程序的中每个函数和方法的运行时间和内存情况,都记录到profiler中,但是需要专门的工具去分析,PhpStorm就提供了这种工具,下面给介绍下使用Phpstorm和Xdebug分析和优化程序。