简体   繁体   中英

Laravel - How to store data from dynamic form field?

I want to store data from the dynamic form field.

And this is my code:

My Controller:

 /*
 * This is For Create Question
 *
 */
public function CreateQuestion(Request $request)
{
    $this->validate($request, [
        'exercise' => 'required',
        'question' => 'required',
        'opt1' => 'required',
        'opt2' => 'required',
        'opt3' => 'required',
        'opt4' => 'required',
    ]);

    $inputs = $request->all();

    foreach ($inputs as $input)
        {
            Question::create(array(
                'exercise'=>$input['exercise'],
                'question'=>$input['question'],
                'opt1'=>$input['opt1'],
                'opt2'=>$input['opt2'],
                'opt3'=>$input['opt3'],
                'opt4'=>$input['opt4'],
            ));
        }

    return back()->with('success','Soal Berhasil dikirim.');
}

and I use names like exercise[] for the form.

And the result of my code is:

 ErrorException
 Illegal string offset 'exercise'

If you're looping over $inputs , then you're essentially trying to create a Question record for every form input... Don't do that. Get rid of the foreach() and access the inputs properly:

Question::create(array(
    'exercise' => $request->input('exercise'),
    'question' => $request->input('question'),
    'opt1' => $request->input('opt1'),
    'opt2' => $request->input('opt2'),
    'opt3' => $request->input('opt3'),
    'opt4' => $request->input('opt4')
));

I updated my code, here I can get all the values. But it appears like this:

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of 
the type array, string given,

My New Controller:

 /*
 * This is For Create Question
 *
 */
public function createQuestion(Request $request)
{
    $request->validate([
        'exercise' => 'required',
        'question' => 'required',
        'opt1' => 'required',
        'opt2' => 'required',
        'opt3' => 'required',
        'opt4' => 'required',
    ]);

    //Init
    $inputs = $request->all();


    Question::create(array(
        'exercise' => $inputs['exercise'],
        'question' => $inputs['question'],
        'opt1' => $inputs['opt1'],
        'opt2' => $inputs['opt2'],
        'opt3' => $inputs['opt3'],
        'opt4' => $inputs['opt4'],
    ));

    return back()->with('success','Soal Berhasil dikirim.');
}

My New form:

 <form>
   <select name="exercise[]"></select>
   <textarea name="question[]"></textarea>
   <input name="opt1[]"></input>
   <input name="opt2[]"></input>
   <input name="opt3[]"></input>
   <input name="opt4[]"></input>
   <!-- This is the add more filed button -->
   <button name="addMoreSoal" id="addMoreSoal"addMoreSoal">Add More 
   Soal</button> 
   <button name="submit" type="submit">Upload</button>
 </form>

And thanks to all who have tried to help me, but I need more help for this one...

As @Tim Lewis said, "you are trying to create a Question record for every form input", another way, you can pass all the inputs inside the create method to insert a new question record to the database

public function CreateQuestion(Request $request)
{
    $this->validate($request, [
        'exercise' => 'required',
        'question' => 'required',
        'opt1' => 'required',
        'opt2' => 'required',
        'opt3' => 'required',
        'opt4' => 'required',
    ]);

    $inputs = $request->all();

    Question::create($inputs);

    return back()->with('success','Soal Berhasil dikirim.');
}
public function CreateQuestion(Request $request)
{
    $this->validate($request, [
        'exercise' => 'required',
        'question' => 'required',
        'opt1' => 'required',
        'opt2' => 'required',
        'opt3' => 'required',
        'opt4' => 'required',
    ]);

    $inputs = $request->all();

    foreach ($inputs['exercise'] as $key => $exercise)
        {
            Question::create(array(
                'exercise'=>$exercise,
                'question'=>$inputs['question'][key],
                'opt1'=>$inputs['opt1'][key],
                'opt2'=>$inputs['opt2'][key],
                'opt3'=>$inputs['opt3'][key],
                'opt4'=>$inputs['opt4'][key],
            ));
        }

    return back()->with('success','Soal Berhasil dikirim.');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM