Summer Sale! All accounts are 50% off this week.

PaulCatalin97's avatar

undefined index

i`m trying to insert some data in model and i get this error

this is my form for this specific column

 <div class="dropdown-field">
                                    <select data-placeholder="Selecteaza Specializare" class="chosen" id="skills" name="skills" multiple="multiple">
                                        @foreach($skills as $skill)
                                            <option value="{{$skill->id}}" >{{strtoupper($skill->name)}}</option>
                                        @endforeach
                                    </select>
                                    <div class="box-footer">

and this is the controller

 class AdaugaJobController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $skills = SkillsEmployee::all();
        return view('adaugajob', compact('skills'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'titlu' => 'required|string|max:255',
            'descriere' => 'required|string|max:255',
            'salariu_estimativ' => 'required|string|max:255',
            'oras'=> 'required',
            'id_skill'=>'',

        ]);

        $skills = $data['skills[]'];

        foreach($skills as $skill){
            Joburi::create([
                'titlu' => $data['titlu'],
                'descriere' => $data['descriere'],
                'salariu_estimativ' => $data['salariu_estimativ'],
                'oras' => $data['oras'],
                'id_skill'=>$skill,
            ]);
        }
        if($data){
            return redirect()->route('adaugajob')->withSuccess('S-a incarcat cu success!');
        }else{
            return redirect()->route('adaugajob')->withDanger('Nu s-a incarcat! A aparut o eroare.');
        }
        }
    }

0 likes
14 replies
tisuchi's avatar

I think it needs to be an array name="skills[]". If I am not mistaken, you are trying to choose more than 1 skills at a time.

<select data-placeholder="Selecteaza Specializare" class="chosen" id="skills" name="skills[]" multiple="multiple">

And in your store method, you need to update like this way-

$skills = $data['skills'];

foreach($skills as $skill){
    Joburi::create([
            'titlu' => $data['titlu'],
            'descriere' => $data['descriere'],
            'salariu_estimativ' => $data['salariu_estimativ'],
            'oras' => $data['oras'],
            'is_skill'=>$skill,
        ]);
}
tisuchi's avatar

@PAULCATALIN97 - It depends on what you want. If you want to allow the user to choose more than one skill at a time, then you need to do like that way.

I have updated my answer. Check this.

tisuchi's avatar

@paulcatalin97

I just update your whole code here...

<div class="dropdown-field">
    <select data-placeholder="Selecteaza Specializare" class="chosen" id="skills" name="skills[]" multiple="multiple">
        @foreach($skills as $skill)
            <option value="{{$skill->id}}" >{{strtoupper($skill->name)}}</option>
        @endforeach
    </select>
<div class="box-footer">

Here is the controller-

class AdaugaJobController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $skills = SkillsEmployee::all();
        return view('adaugajob', compact('skills'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'titlu' => 'required|string|max:255',
            'descriere' => 'required|string|max:255',
            'salariu_estimativ' => 'required|string|max:255',
            'oras'=> 'required',
            'id_skill'=>'',

        ]);

        $skills = $data['skills'];

        foreach($skills as $skill){
            Joburi::create([
                'titlu' => $data['titlu'],
                'descriere' => $data['descriere'],
                'salariu_estimativ' => $data['salariu_estimativ'],
                'oras' => $data['oras'],
                'id_skill'=>$skill,
            ]);
        }
        if($data){
            return redirect()->route('adaugajob')->withSuccess('S-a incarcat cu success!');
        }else{
            return redirect()->route('adaugajob')->withDanger('Nu s-a incarcat! A aparut o eroare.');
        }
        }
    }

I think it should work.

1 like
tisuchi's avatar

@PAULCATALIN97 - I haven't noticed that you have received data from the user in the wrong way. It should be like that. Can you try with this code in your controller?

$skills = $request->input('skills');

        foreach($skills as $skill){
            Joburi::create([
                'titlu' => $request->input('titlu'),
                'descriere' => $request->input('descriere'),
                'salariu_estimativ' => $request->input('salariu_estimativ'),
                'oras' => $request->input('oras'),
                'id_skill'=>$skill,
            ]);
        }
PaulCatalin97's avatar

@TISUCHI - solved it, in the validation was the problem, the index was defined 'id_skill' and we tried to fill it the skill field

1 like
jlrdw's avatar

Could you mark as solved so others know. Others will read over all this to see if they can help only to discover it's solved.

Please or to participate in this conversation.