0

I have a problem updating a field in my User Model.

I have a field for activating a public_profile defined as boolean:

...
$table->boolean('public_profile')->default(false);
... 

in the Model itself i define.

protected $fillable = [..., 'public_profile', ...];

Now i try to update the field via axios depending on the status of an checkbox.

$('#change_public_profile').change(function () {
    message={
       _token: $('meta[name="csrf-token"]').attr('content'),
       public_profile: $('#change_public_profile').is(':checked')
}
axios.post('SOME URL', message).....

Now in my controller i read the request:

$public_profile = $request->input('public_profile');

And call the function:

Auth::user()->togglePublicProfile($public_profile);

In the User Model I have the function:

public function togglePublicProfile($toggleTo){
    $user = $this;
    $user->public_profile = $toggleTo;
    return $user->save();
}

I also tried with boolval() as Mutator, but it never changed. I do the same to chagne a string value, and there it works well.

Where is my mistake?

Jakob Graf
  • 376
  • 1
  • 3
  • 14
  • If you wont find a proper fix: I would use TINYINT instead of BOOLEAN. Basiclly BOOLEAN is a synonyms of TNIYINT http://stackoverflow.com/questions/289727/which-mysql-datatype-to-use-for-storing-boolean-values#289759 So better work fully with `1` and `0` in your code not with boolean-values like `true/false` – JustOnUnderMillions May 02 '17 at 15:47
  • @JustOnUnderMillions Laravel's `$table->boolean()` creates the column to `tinyint(1)`. – aynber May 02 '17 at 15:54
  • Try some logging in the togglePublicProfile, and see what `$toggleTo` is, and what `$user->public_profile` is before and after assigning. – aynber May 02 '17 at 15:55
  • I thing you have to check in what format the value is send by ajax.post. Sometimes an true is send as string 'true' and then 'false' is also true later in the database. Check ajax.post data with : `F12` developerconsole network-tab request-data – JustOnUnderMillions May 02 '17 at 15:56
  • I definitely recive not a string in my php script, i tryed to resonse the is_bool($public_profile); and i also called the function like Auth::user()->togglePublicProfile(true); and also Auth::user()->togglePublicProfile(1); I will try the logging befroe and after the saving, thanks – Jakob Graf May 02 '17 at 16:19
  • Just a thought, how are you testing the update? If you're not checking the database, and instead relying on the front end that might be misleading. I know that when you pull bool values into javascript, you need to be careful about types as Laravel defaults to returning bools as string types unless you transform (cast) them. I would definitely add some logging before and after the save statement to make sure it's actually saving. – AndreLiem May 02 '17 at 20:45

1 Answers1

1

Since you have boolean (true/false) you dont need to get any input ($toggleTo) because you only have two states. Each time you call this option you can set the opposite value.

$this->public_profile = !$this->public_profile;
$this->save();
Tim
  • 446
  • 1
  • 5
  • 17