我正在try 根据邮箱not there id更新用户,有没有一种方法可以在没有原始查询的情况下进行更新.

Currently the error

{"error":{"type":"ErrorException","message":"Creating default object from empty value","file":"C:\wamp\www\celeb-jim\app\controllers\SignupController.php","line":189}}

My Code

    public function changeAccountStatus ($plan, $userEmail ){
        $UpdateDetails = User::where('email', '=',  $userEmail)->first();
        $UpdateDetails->member_type = $plan;
        $UpdateDetails->save();
    }

推荐答案

此错误表明User::where('email', '=', $userEmail)->first()返回null,而不是更新模型时出现问题.

在try 更改用户的属性或使用firstOrFail()方法之前,请判断是否确实有用户.

$UpdateDetails = User::where('email', $userEmail)->first();

if (is_null($UpdateDetails)) {
    return false;
}

或者使用firstOrFail()方法,不需要判断用户是否为null,因为当找不到模型时,这会引发异常(ModelNotFoundException),您可以使用App::error() http://laravel.com/docs/4.2/errors#handling-errors捕捉该异常

$UpdateDetails = User::where('email', $userEmail)->firstOrFail();

Laravel相关问答推荐

保存很多模型太慢

LaravelEloquent 的模型如何将值从控制器内部的函数传递到Render()

如何在 Laravel 中使用 return 停止 Trait php 的执行

将 ID 传递给资源控制器进行编辑

在 laravel 5.3 中截断的错误日志(log)

Laravel 有Many Many to Many To One Eloquent

Laravel Blade:@stop VS @show VS @endsection VS @append

Laravel 5文件夹 struct 中的文件保存在哪里?

验证规则 required_if 与其他条件(Laravel 5.4)

Laravel 5 - 仅在特定页面/控制器(页面特定assets资源)上添加样式表

Laravel 4:读取由 javascript 设置的 cookie

PHP 5.5,什么情况下PHP会导致很高的committed memory

带有时间戳的 Laravel 5.1 eloquent::attach() 方法

laravel 5 - assets资源 list 中未定义 css 文件?

413请求实体在laravel homestead for windows中太大的nginx服务器

Http请求多浏览器的烦恼

Laravel 初学者的良好开端

如果 Laravel 中的值不为空,如何验证输入字段

Laravel 5.2 验证错误未出现在Blade中

没有Access-Control-Allow-Origin标头 - Laravel