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

时间:2022-10-21
本文介绍了Laravel 5 mutator 仅在我创建记录时有效,而在我更新记录时无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我创建了一个只在我的电话号码上存储数字的 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 AppHttpControllers;
use AppHttpRequests;
use AppHttpRequestsProfileRequest;
use AppHttpControllersController;
use IlluminateHttpRequest;
use Auth;
use AppProfile;

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()) 调用关系的 create 方法(HasOneOrMany).此方法然后创建一个相关模型的新实例.这很重要,因为显然属性修改器仅在通过模型创建记录时使用.

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 调用被代理到查询构建器实例(匹配关系).这会导致执行以下内容:

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]

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

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 仅在我创建记录时有效,而在我更新记录时无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:Laravel 时间戳显示毫秒 下一条:在 Laravel 中使用 Sweet Alert 删除方法

相关文章

最新文章