2

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?

blank94
  • 374
  • 3
  • 20
  • You should include what error you're getting. anyways users table have id as a primary key so you cannot make it all same `//it's zero for all users` and if you are using default laravel migration, user->id can only be integer value (not crypted). – Maulik Aug 05 '17 at 04:37
  • I have no error.I'm using laravel default migration , I just want to encrypt id in my code not in database!@MaulikGangani see the modified code – blank94 Aug 05 '17 at 04:45
  • Settings the ip without saving the domain model should not result in an error when using laravel's default migration. If you save the model, it can be saved as 0 or result in an error. – wartoshika Aug 05 '17 at 05:28

2 Answers2

0

Try this:

$user->attributes['id'] = Crypt::encrypt(['id'=> $user->id]);

A Laravel getter and settier in eloquent doesnt allow changing some object properties. The attributes object is a wrapper of the object's real properties.

wartoshika
  • 531
  • 3
  • 10
  • It will give `PHP error: Indirect modification of overloaded property App\User::$attributes has no effect on line 1` and no change in $user->id – Maulik Aug 05 '17 at 05:40
  • yeah I get the same error that @MaulikGangani gets. – blank94 Aug 05 '17 at 05:49
0

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!

Maulik
  • 2,881
  • 1
  • 22
  • 27