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;
    }
}
OsvyG
  • 315
  • 2
  • 8

2 Answers2

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;
}
lukasgeiter
  • 124,981
  • 24
  • 288
  • 251
  • Thanks! this makes a lot of sense. This fixed my issue: $saveToDatabase = $profile->update($request->all()); – OsvyG Mar 27 '15 at 19:09
1

Using this code instead of your code

$saveToDatabase = Auth::user()->profile->update($request->all());
mininoz
  • 5,728
  • 4
  • 21
  • 23