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

时间:2015-03-27 17:40:59

标签: php laravel laravel-5 mutators

您好我已经创建了一个mutator,只在我的手机号码上存储数字。这是我的个人资料模型中的代码。

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

2 个答案:

答案 0 :(得分:19)

这里发生了什么:

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]

根本没有使用该模型。因此,mutator不起作用。

相反,您必须在实际相关模型上调用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;
}

答案 1 :(得分:0)

使用此代码代替您的代码

$saveToDatabase = Auth::user()->profile->update($request->all());