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?
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.
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.
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.