Search code examples
phplaravel-5eloquentlaravel-encryption

can't overwrite laravel elequent output's id field


I can't overwrite the $user->id in the following code :

$followers_list = follow::where('followed_id',$userId['userId'])
            ->get();   

        foreach($followers_list as $follower)
        {
           $user = myuser::find($follower->follower_id);
           echo $user->id;//everything is fine
           $user->id = Crypt::encrypt(['id'=> $user->id]);               
           echo $user->id; //it's zero for all users
           array_push($followers,$user);               
        }

is it a rule or something in laravel eloquent to prevent such type conversions(integer to string)?

how can I replace the id's integer value with its encrypted string?

any help?


Solution

  • laravel will not allow to change datatype of PrimaryKey on the fly on any models,

    how do I know it? check YourProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:57

    enter image description here

    Now if you want to do it, you can call $user->setKeyType('string') and then do your $user->id = 'hihello';

    is it recommended? I don't know!