0

I am new to laravel

I have two columns in one of my table id,role_id,

and I have two arrays

$id = array(1,2,3,4,5,6,....);

$role_id = array(4,5,6,7,8,....);

what I want to do is update the role_id column according to the given id array;

for example I want role_id 4 against id 1,5 against 2

is there any way to do it in laravel

Any help will be appreciated

1 Answer 1

1

If the sizes or lengths of both the arrays are equal, then you can do it like this:

for($counter = 0; $counter< count(any of the two arrays); $counter++){
$tablename = new Tablename();
$tablename->id = $id[$counter];
$tablename->role_id = $role_id[$counter];
$tablename->save();
}

and to update:

for($counter = 0; $counter< count(any of the two arrays); $counter++){
Tablename::where('id', $id[$counter])
  ->update(['role_id' => $role_id[$counter]]);
}
Sign up to request clarification or add additional context in comments.

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.