Laravel 5 mutator 仅在我创建记录时有效,而在我更新记录时无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29306890/
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-09-14 11:14:30  来源:igfitidea点击:

Laravel 5 mutators only work when I create a record and not when I update a record

phplaravellaravel-5mutators

提问by OsvyG

Hi I have created a mutator to only store digits on my phone numbers. Here is my code in my Profile Model.

嗨,我创建了一个仅在我的电话号码上存储数字的 mutator。这是我的个人资料模型中的代码。

public function setPhoneAttribute($phone)
{
    $this->attributes['phone'] = preg_replace("/[^0-9]/","",$phone);
}

This works when I create a new record, but if I update the record it does not work. My question is how do I execute the Mutator on both create and update?

这在我创建新记录时有效,但如果我更新记录,则它不起作用。我的问题是如何在创建和更新时执行 Mutator?

Here is how I update and create in my controller:

这是我在控制器中更新和创建的方法:

namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Requests\ProfileRequest;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Profile;

class ProfileController extends Controller {

    public function create(ProfileRequest $request)
    {
        // Check if the user does not have a profile yet
        if(!Auth::user()->profile()->first()){

            // Save to database
            $saveToDatabase = Auth::user()->profile()->create($request->all()); 

            return $saveToDatabase;
        }
    }

    public function update(Profile $profile, ProfileRequest $request)
    {

        // Save to database
        $saveToDatabase = Auth::user()->profile()->update($request->all());

        return $saveToDatabase;
    }
}

回答by lukasgeiter

Here's what's happening:

这是发生了什么:

Auth::user()->profile()->create($request->all())calls the createmethod on your relationship (HasOneOrMany). This method then creates a new instance of the related model. This is important because obviously attribute mutators are only used when the record is created through the model.

Auth::user()->profile()->create($request->all())create在您的关系 ( HasOneOrMany)上调用该方法。此方法然后创建相关模型新实例。这很重要,因为显然只有在通过模型创建记录时才会使用属性修改器。

However the relationship object doesn't have any updatemethod. (It also wouldn't make sense to have one...). So what's happening instead is, when you do Auth::user()->profile()->update($request->all()). The updatecall get's proxied off to a query builder instance (that matches the relationship). This results in something like this being executed:

然而,关系对象没有任何update方法。(拥有一个也没有意义......)。所以发生的事情是,当你这样做时Auth::user()->profile()->update($request->all())。该update调用get的代理开了一个查询生成器实例(即关系相符)。这会导致执行以下操作:

UPDATE profiles SET foo = 'bar' WHERE [relationship conditions]

It doesn't use the model at all. Therefore the mutator doesn't work.

它根本不使用模型。因此,mutator 不起作用。

Instead you have to call the updatemethod on the actual related model. You can access it by just calling the relation as a property like this:

相反,您必须update在实际相关模型上调用该方法。您可以通过将关系作为属性调用来访问它,如下所示:

$saveToDatabase = Auth::user()->profile->update($request->all());
//                                    ^^
//                               no parentheses


If the Profilemodel is injected correctly you actually might also just use that though:

如果Profile模型被正确注入,你实际上也可以使用它:

public function update(Profile $profile, ProfileRequest $request)
{
    // Save to database
    $saveToDatabase = $profile->update($request->all());
    return $saveToDatabase;
}

回答by mininoz

Using this code instead of your code

使用此代码而不是您的代码

$saveToDatabase = Auth::user()->profile->update($request->all());