Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Djbethel's avatar

Passing Form Array while using Laracasts\Commander

So I'm using the laracasts commander package and using the 'magic' execute method. However I have an array I need to pass from my form, any idea how I can do this?

For example, in my firm a user can have many 'Skills", Skills are comprised of skill_name's and skill_level's

so in my form I add the [] next to skillName and skillLevel to make them an array. But how do I tell Laracasts Commander that I am expecting my $skills variable to be an array with properties skillName and skillLevel?

Any help would be nice! Cheers

0 likes
18 replies
FrancescoZaffaroni's avatar

If i get what you mean try naming the input like this:

<input name="skills[0][skill_name]"/>
<input name="skills[0][skill_level]"/>
<input name="skills[1][skill_name]"/>
<input name="skills[1][skill_level]"/>
Etc

Hope this helps.

P.s. Gosh its annoying writing html with an ipad

Djbethel's avatar

Hm, I get that but how would I assign the skillName and skillLevel values ? I'm assuming a foreach loop but how do I grab the array key and values from commander.

1 like
Djbethel's avatar

Hm, I have binded the array to one of the command properties. However, when I try to 'foreach" through the $command->skills array and try something like

$skill->name = $command->skills['skillName']

I get an Illegal string offset error.

However if I dd($command->skills) I can see the entire array.

FrancescoZaffaroni's avatar

Try something like

foreach($command->skills as $data)
{
 $skill->name = $sata['skillName'];
}
Djbethel's avatar

Sure, not exactly positive how to post code on the new forums yet but the foreach loop goes like

foreach($data->skills as $skills)
{
    $skill = new Skill;
    $skill->name = $skills['skillName'];
    $skill->level = $skills['skillLevel'];
    $skill->save();
}

Now it is only saving one entry when it should save both. Also note I am using indexes on the form elements like you have suggested.

Djbethel's avatar

Well, yeah sorry for the messy code but my code looks pretty much like what you have there.

FrancescoZaffaroni's avatar

Can i see the var dump? To post code wrap it in three accents caracter ( see github flavored markdown)

Djbethel's avatar

Ok, what exactly do you want the var_dump of? The foreach loop code you posted?

Djbethel's avatar
array (size=2)
  0 => 
    array (size=2)
      'skillName' => string 'Adobe Photoshop' (length=15)
      'skillLevel' => string '1' (length=1)
  1 => 
    array (size=2)
      'skillName' => string 'Adobe Fireworks' (length=15)
      'skillLevel' => string '1' (length=1)
Djbethel's avatar

I tried doing the foreach loop to include the $key, and then tried something like

foreach($data->skills as $key => skills)
{
$skills[$key]['skillName'];
}

But now I get an Undefined Offset error. But if I try the inner code outside of a foreach loop, it gives me the correct value for the index. Weird.

Djbethel's avatar

If I run this loop

        foreach($data->skills as $skills)
        {
            dd($skills['skillName']);
        }

The result is only the first item in the array, not all of them, as follows.

string 'Adobe Photoshop' (length=15)
Djbethel's avatar

This is where I am right now with my code. However, still only the first item is being added to the database.

        foreach(array($data->skills) as $key => $skills)
        {
            $skill = new Skill;
            $skill->name = $skills[$key]['skillName'];
            $skill->level = $skills[$key]['skillLevel'];
            $skill->save();
            $resume->skill()->attach($skill->id);
        }

I added the array() method because I think $data variable comes in as a string via commander. Which is why I was getting the undefined offset within the loop earlier. The array() method fixes that, however only one of my 'skills' is being saved to the database.

Any further help will be greatly appreciated :)

FrancescoZaffaroni's avatar

Well, you are messing it up a little. You should get rid of array() in the foreach call and of all the $key stuff.

In this example

        foreach($data->skills as $skills)
        {
            dd($skills['skillName']);
        }

You get only the first result because dd()is a shortand for die() and var_dump(), and die() will stop the execution. Try to use var_dump() instead.

And make sure you get rid of all dd()s in the function where you save.

Please or to participate in this conversation.