久久久久久久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)
主站蜘蛛池模板: www久久av| 国产女人与拘做受免费视频 | 一级片免费网站 | 九九精品久久久 | 国产精品亚洲精品 | 中日韩欧美一级片 | 久久免费精品视频 | 中文字幕综合在线 | 国产精品久久一区二区三区 | 久久久xxx | 色网站视频 | 精品在线一区二区三区 | 人人射人人插 | 日产久久| 日韩在线国产 | 日日夜夜精品免费视频 | 亚洲色图在线观看 | 欧美极品在线 | 欧美精品片 | 国产www在线 | 欧美一区二区三区 | 日韩精品一区二区三区中文在线 | 国产视频一区二区在线观看 | 啪一啪| 色婷婷久久久久swag精品 | av在线一区二区三区 | 国产男人的天堂 | 国内久久精品 | 亚洲成人av在线播放 | 美女毛片免费看 | 成人免费视频网站 | 在线观看国产视频 | 国产精品区二区三区日本 | 深夜福利亚洲 | 亚洲国产aⅴ成人精品无吗 综合国产在线 | 国产精品免费一区二区 | 欧州一区二区三区 | 久久91视频 | 91精品国产91久久久久久最新 | 欧美日韩在线一区二区三区 | 久久亚洲高清 |