0

does someone can help me to show me how can I make a loop if my object is just like this assigned: "["741852963","79545236","7845120"]"

I try with

$assigned = array();
    foreach($project->assigned as $arrayItem){

        $user = Admin::select('name', 'numero_empleado')->where('numero_empleado', $arrayItem)->get();
        $assigned =  $user;
    }
return $assigned;

but that way throw me an error

"Invalid argument supplied for foreach()"

I want to add the result in other array to send to the front

1
  • $project->assigned looks like a string representation of an array... Which is not valid in a foreach() loop. Run dd(is_string($project->assigned));; if that says true, then that's your issue. Commented Mar 27, 2021 at 2:55

2 Answers 2

1

You have to first json_decode

$myData = json_decode($project->assigned);

Then use foreach

foreach($myData as $arrayItem){

    $user = Admin::select('name', 'numero_empleado')->where('numero_empleado', $arrayItem)->get();
    $assigned =  $user;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not only that, but he has also to set that attribute as an Array casting it...
0

You have to convert string array into array , you can use json_decode method like:-

$assigned = json_decode($assigned);

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.