1

I want to create a value for a variable $user_role->role_id in php.Is it possible to create a variable as $user_role->role_id which can be parse in php I have tried as

$user_role=array(); 
$user_role->role_id='1';

getting error in

print_r($user_role->role_id);

error I got

Attempt to assign property of non-object

2
  • 6
    You're trying to assign an object property to an array. If you have a Laravel model you need to invoke it in the first line rather than creating a blank array. E.g. $user_role = new App\Role If you want to populate an array then you just do $user_role['role_id'] = 1;. Commented May 10, 2018 at 1:53
  • plz post ur answer.I'll accept it. Commented May 10, 2018 at 2:00

3 Answers 3

2

You're trying to assign an object property to an array. If you have a Laravel model you need to invoke it in the first line rather than creating a blank array. E.g. $user_role = new App\Role If you want to populate an array then you just do $user_role['role_id'] = 1;

Sign up to request clarification or add additional context in comments.

Comments

2

You are creating array variable and fetch by object. That's why this error comming.

$user_role          = array();
$user_role['role_id'] = '1';

//print value
echo  $user_role['role_id'];

Comments

0

Are you using any User Role package to manage this or just trying on your own?

If any package, check for document on how to initialize the code for role assignment.

Still what we see, you are trying to perform Eloquent operation and wishes to put role_id, so if its a User who has one Role, probably this would be your code

$user = new App\User; // You could also initialize by fetching the record
$user->role_id = 1; // Assigning role_id as `1`
$user->save();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.