Laravel 5 mutators only work when I create a record and not when I update a record
Asked Answered
S

2

12

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

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?

Here is how I update and create in my controller:

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;
    }
}
Saturninasaturnine answered 27/3, 2015 at 17:40 Comment(1)
Can you please edit your question and add the code that shows how you update/create the recordAerology
A
23

Here's what's happening:

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.

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]

It doesn't use the model at all. Therefore the mutator doesn't work.

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

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;
}
Aerology answered 27/3, 2015 at 18:58 Comment(1)
Thanks! this makes a lot of sense. This fixed my issue: $saveToDatabase = $profile->update($request->all());Saturninasaturnine
A
1

Using this code instead of your code

$saveToDatabase = Auth::user()->profile->update($request->all());
Artistic answered 27/3, 2015 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.