user3386779
user3386779

Reputation: 7175

create a array variable for laravel

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

Upvotes: 1

Views: 13464

Answers (3)

Vipul Dhandhresha
Vipul Dhandhresha

Reputation: 54

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'];

Upvotes: 2

rsakhale
rsakhale

Reputation: 1036

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();

Upvotes: 0

lufc
lufc

Reputation: 2040

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;

Upvotes: 2

Related Questions