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

Laravel 5 mutators only work when I create a record and not when I update a record(Laravel 5 mutator 仅在我创建记录时有效,而在我更新记录时无效)
本文介绍了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 仅在我创建记录时有效,而在我更新记录时无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)