久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 5 mutator 僅在我創(chuàng)建記錄時有效,而在我更

Laravel 5 mutators only work when I create a record and not when I update a record(Laravel 5 mutator 僅在我創(chuàng)建記錄時有效,而在我更新記錄時無效)
本文介紹了Laravel 5 mutator 僅在我創(chuàng)建記錄時有效,而在我更新記錄時無效的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我創(chuàng)建了一個只在我的電話號碼上存儲數(shù)字的 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);
}

這在我創(chuàng)建新記錄時有效,但如果我更新記錄則不起作用.我的問題是如何在創(chuàng)建和更新時執(zhí)行 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?

以下是我在控制器中更新和創(chuàng)建的方法:

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()) 調(diào)用關(guān)系的 create 方法(HasOneOrMany).此方法然后創(chuàng)建一個相關(guān)模型的新實例.這很重要,因為顯然屬性修改器僅在通過模型創(chuàng)建記錄時使用.

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.

然而,關(guān)系對象沒有任何 update 方法.(擁有一個也沒有意義......).因此,當(dāng)您執(zhí)行 Auth::user()->profile()->update($request->all()) 時,會發(fā)生什么.update 調(diào)用被代理到查詢構(gòu)建器實例(匹配關(guān)系).這會導(dǎo)致執(zhí)行以下內(nèi)容:

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.

相反,您必須在實際相關(guān)模型上調(diào)用 update 方法.您可以通過將關(guān)系作為屬性調(diào)用來訪問它:

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

這篇關(guān)于Laravel 5 mutator 僅在我創(chuàng)建記錄時有效,而在我更新記錄時無效的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Magento products by categories(按類別劃分的 Magento 產(chǎn)品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 99爱国产 | 91久久精品国产91久久性色tv | 一级毛片免费完整视频 | 黑人巨大精品欧美一区二区免费 | 黄色一级电影在线观看 | 国产亚洲一区二区三区 | 亚洲小说图片 | 欧美视频区 | 国产欧美精品一区二区 | 午夜精品久久久久久久 | 羞羞在线视频 | 一区二区三区视频在线 | 国产精品美女 | 国内精品久久久久久 | 精品小视频 | 性生生活大片免费看视频 | 99久久久99久久国产片鸭王 | 黑人久久 | 日日干干 | 玖玖久久 | 337p日本欧洲亚洲大胆 | 亚洲精品日韩一区二区电影 | 亚洲二区视频 | 国产精品综合 | 亚洲精品www | 欧美激情视频一区二区三区免费 | 国产精品久久久久久久久久久久久久 | 毛片免费看 | 国产精品久久久久久久岛一牛影视 | 久久精品国产一区二区电影 | 黄色在线免费观看视频网站 | 精品国产乱码久久久久久老虎 | 亚洲成人精 | 国产精品爱久久久久久久 | 在线播放国产一区二区三区 | 欧美精品乱码久久久久久按摩 | 久久精品99 | 免费观看一区二区三区毛片 | 久久久黑人 | 超碰精品在线 | 欧美日韩精品免费观看 |