php Laravel 更新查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27248753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:15:44  来源:igfitidea点击:

Laravel Update Query

phplaravel

提问by Brent

I am trying to update a User on the basis of the email not there id, is there a way of doing this without raw queries.

我正在尝试根据电子邮件而不是 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}}

{"error":{"type":"ErrorException","message":"从空值创建默认对象","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();
    }
    public function changeAccountStatus ($plan, $userEmail ){
        $UpdateDetails = User::where('email', '=',  $userEmail)->first();
        $UpdateDetails->member_type = $plan;
        $UpdateDetails->save();
    }

采纳答案by Wader

This error would suggest that User::where('email', '=', $userEmail)->first()is returning null, rather than a problem with updating your model.

此错误表明User::where('email', '=', $userEmail)->first()返回空值,而不是更新模型的问题。

Check that you actually have a User before attempting to change properties on it, or use the firstOrFail()method.

在尝试更改其属性或使用该firstOrFail()方法之前,请检查您是否确实拥有用户。

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

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

or using the firstOrFail()method, theres no need to check if the user is null because this throws an exception (ModelNotFoundException) when a model is not found, which you can catch using App::error()http://laravel.com/docs/4.2/errors#handling-errors

或使用该firstOrFail()方法,无需检查用户是否为空,因为ModelNotFoundException在找不到模型时会抛出异常 ( ),您可以使用http://laravel.com/docs/4.2/errors#handling-捕获该异常App::error()错误

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

回答by msturdy

You could use the Laravel query builder, but this is not the best way to do it.

您可以使用Laravel 查询构建器,但这不是最好的方法

Check Wader's answer belowfor the Eloquent way - which is better as it allows you to check that there is actually a user that matches the email address, and handle the error if there isn't.

检查下面的 Wader对 Eloquent 方式的回答- 这更好,因为它允许您检查是否确实存在与电子邮件地址匹配的用户,如果没有,则处理错误。

DB::table('users')
        ->where('email', $userEmail)  // find your user by their email
        ->limit(1)  // optional - to ensure only one record is updated.
        ->update(array('member_type' => $plan));  // update the record in the DB. 

If you have multiple fields to update you can simply add more values to that array at the end.

如果您有多个要更新的字段,您只需在最后向该数组添加更多值即可。

回答by Cengkuru Michael

Try doing it like this.

尝试这样做。

User::where('email', $userEmail)
       ->update([
           'member_type' => $plan
        ]);

回答by nandu

$update = \DB::table('student') ->where('id', $data['id']) ->limit(1) ->update( [ 'name' => $data['name'], 'address' => $data['address'], 'email' => $data['email'], 'contactno' => $data['contactno'] ]); 

回答by rashedcs

It is very simple to do. Code are given below :

做起来很简单。代码如下:

 DB::table('user')->where('email', $userEmail)->update(array('member_type' => $plan));