A
A
asdasdqwe2021-11-17 16:49:11
Laravel
asdasdqwe, 2021-11-17 16:49:11

How to write array value to laravel base?

$request->except('_token');
619506271b584031633931.png

public function store(ApplicantSkillsRequest $request)
    {
        $applicant = $request->user();
        $params = $request->except('_token');

        ApplicantSkill::create([
            'user_id' => $applicant->id,
            'name' => $params['name'],
            'level' => $params['level'],
        ]);

        return redirect()->route('applicant.skills.index');
    }


modelUser.php
public function skills()
    {
        return $this->hasMany(ApplicantSkills::class);
    }


As a result, the error is: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, int given

There should be two entries in the applicant_skills table:
user_id      name       level
1             js           55
1             php          85

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asdasdqwe, 2021-11-17
@asdasdqwe

This is how it works, though I don’t know if there is a laravel method that allows you to do this in one line

public function store(ApplicantSkillsRequest $request)
    {
        $applicant = $request->user();
        $params = $request->except('_token');

        $levels = $request->level;

        foreach($request->name as $i => $name) {
            $skill = new ApplicantSkill;
            $skill->user_id = $applicant->id;
            $skill->name = $name;
            $skill->level = $levels[$i];

            $skill->save();
        }

        return redirect()->route('applicant.skills.index');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question