Laravel 5 mutator仅在创建记录时起作用,而在更新记录时不起作用 [英] Laravel 5 mutators only work when I create a record and not when I update a record

查看:171
本文介绍了Laravel 5 mutator仅在创建记录时起作用,而在更新记录时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我创建了一个转换器,仅将数字存储在我的电话号码中.这是我的个人资料模型中的代码.

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

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

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

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?

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

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;
    }
}

推荐答案

这是正在发生的事情:

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

Auth::user()->profile()->create($request->all()) calls the create method 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.

但是,关系对象没有任何update方法. (拥有一个也没有意义...).因此,发生的是,当您执行Auth::user()->profile()->update($request->all())时. update调用get被代理到查询构建器实例(匹配关系).这样会导致执行以下操作:

However the relationship object doesn't have any update method. (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 update call get's proxied off to a query builder instance (that matches the relationship). This results in something like this being executed:

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

它完全不使用模型.因此,增幅器不起作用.

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

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

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

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


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


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

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

这篇关于Laravel 5 mutator仅在创建记录时起作用,而在更新记录时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆