Laravel 5 mutators仅在我创建记录时工作,而不是在我更新记录时

Osv*_*vyG 11 php mutators laravel laravel-5

嗨,我已经创建了一个mutator,只在我的电话号码上存储数字.这是我的个人资料模型中的代码.

public function setPhoneAttribute($phone)
{
    $this->attributes['phone'] = preg_replace("/[^0-9]/","",$phone);
}
Run Code Online (Sandbox Code Playgroud)

这在我创建新记录时有效,但如果我更新记录则不起作用.我的问题是如何在创建和更新上执行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;
    }
}
Run Code Online (Sandbox Code Playgroud)

luk*_*ter 20

这是发生了什么:

Auth::user()->profile()->create($request->all())create在你的关系(HasOneOrMany)上调用方法.然后,此方法创建相关模型新实例.这很重要,因为显然属性变换器仅在通过模型创建记录时使用.

但是关系对象没有任何update方法.(拥有一个......也没有意义).所以当你这样做时,发生了什么Auth::user()->profile()->update($request->all()).将update调用get代理到查询构建器实例(与该关系匹配).这导致执行这样的事情:

UPDATE profiles SET foo = 'bar' WHERE [relationship conditions]
Run Code Online (Sandbox Code Playgroud)

它根本不使用该模型.因此,mutator不起作用.

相反,您必须update在实际相关模型上调用该方法.您可以通过将关系称为属性来访问它,如下所示:

$saveToDatabase = Auth::user()->profile->update($request->all());
//                                    ^^
//                               no parentheses
Run Code Online (Sandbox Code Playgroud)

如果Profile正确注入模型,您实际上也可能只使用它:

public function update(Profile $profile, ProfileRequest $request)
{
    // Save to database
    $saveToDatabase = $profile->update($request->all());
    return $saveToDatabase;
}
Run Code Online (Sandbox Code Playgroud)