samedi 25 mars 2017

Slug name and id Laravel 5.4

I try to make a slug attribute for my User model with his name and id in the same time, and to doing that i try to make a saved function in the EventServiceProvider, This function will be executed after save user, code :

User::saved(function($user){
             $user->slug = str_slug($user->name.' '.$user->id);
             $user->save();
         });

But i get a prolem that, the save inside the function make a recursive call for the parent saved function and this make the result be like this

SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'slug' at row 1 (SQL: update candidats set f_name = malek, l_name = aa, email = a@ra.com, password = $2y$10$LD2vRISLv8Bo9va/RarOGOF/7xlg4BjRirOglFw0LB2AEgZiN.B9O, slug = malek-aa-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10, updated_at = 2017-03-25 23:52:32, created_at = 2017-03-25 23:52:32, id = 10 where id = 10)

How i can ignore the saved function call inside ? I need same help to fix that , thanks.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2niSx0i
via IFTTT

Aucun commentaire:

Enregistrer un commentaire