問題描述
我創(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)!