Laravel 5 修改器仅在我创建记录时起作用,而在更新记录时不起作用
Laravel 5 mutators only work when I create a record and not when I update a record
您好,我创建了一个只存储我 phone 号码上的数字的修改器。这是我的配置文件模型中的代码。
public function setPhoneAttribute($phone)
{
$this->attributes['phone'] = preg_replace("/[^0-9]/","",$phone);
}
这在我创建新记录时有效,但如果我更新记录,它就不起作用。我的问题是如何在创建和更新时执行 Mutator?
以下是我在控制器中更新和创建的方式:
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Requests\ProfileRequest;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Profile;
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;
}
}
使用此代码代替您的代码
$saveToDatabase = Auth::user()->profile->update($request->all());
这是正在发生的事情:
Auth::user()->profile()->create($request->all())
对您的关系 (HasOneOrMany
) 调用 create
方法。然后,此方法会创建一个 相关模型的新实例 。这很重要,因为显然只有在通过模型..
创建记录时才使用属性修改器
但是关系对象没有任何 update
方法。 (拥有一个也没有意义......)。因此,当您执行 Auth::user()->profile()->update($request->all())
时,实际发生的情况是。 update
调用 get 被代理到查询构建器实例(匹配关系)。这导致执行类似这样的事情:
UPDATE profiles SET foo = 'bar' WHERE [relationship conditions]
根本不用模型。因此,增变器不起作用。
相反,您必须在实际相关模型上调用 update
方法。您可以通过将关系称为 属性 来访问它,如下所示:
$saveToDatabase = Auth::user()->profile->update($request->all());
// ^^
// no parentheses
如果正确注入 Profile
模型,您实际上也可以只使用它:
public function update(Profile $profile, ProfileRequest $request)
{
// Save to database
$saveToDatabase = $profile->update($request->all());
return $saveToDatabase;
}
您好,我创建了一个只存储我 phone 号码上的数字的修改器。这是我的配置文件模型中的代码。
public function setPhoneAttribute($phone)
{
$this->attributes['phone'] = preg_replace("/[^0-9]/","",$phone);
}
这在我创建新记录时有效,但如果我更新记录,它就不起作用。我的问题是如何在创建和更新时执行 Mutator?
以下是我在控制器中更新和创建的方式:
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Requests\ProfileRequest;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Profile;
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;
}
}
使用此代码代替您的代码
$saveToDatabase = Auth::user()->profile->update($request->all());
这是正在发生的事情:
Auth::user()->profile()->create($request->all())
对您的关系 (HasOneOrMany
) 调用 create
方法。然后,此方法会创建一个 相关模型的新实例 。这很重要,因为显然只有在通过模型..
但是关系对象没有任何 update
方法。 (拥有一个也没有意义......)。因此,当您执行 Auth::user()->profile()->update($request->all())
时,实际发生的情况是。 update
调用 get 被代理到查询构建器实例(匹配关系)。这导致执行类似这样的事情:
UPDATE profiles SET foo = 'bar' WHERE [relationship conditions]
根本不用模型。因此,增变器不起作用。
相反,您必须在实际相关模型上调用 update
方法。您可以通过将关系称为 属性 来访问它,如下所示:
$saveToDatabase = Auth::user()->profile->update($request->all());
// ^^
// no parentheses
如果正确注入 Profile
模型,您实际上也可以只使用它:
public function update(Profile $profile, ProfileRequest $request)
{
// Save to database
$saveToDatabase = $profile->update($request->all());
return $saveToDatabase;
}