2

I have been trying to save data from dynamic form in Laravel 5.3. But I cannot save it as array. The error shows

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

Form:

<select class="form-control-sm" name="client_id[]">
<input type="text" class="form-control-sm" name="amount[]">

Model:

protected $fillable = ['client_id', 'amount'];

public function client()
{
    return $this->belongsTo('App\Client');
}

Controller:

public function store(Request $request)
{
    $count = Client::count();

    $payment = Payment::create(['amount' => $request->amount,
                                 'client_id'  => $request->client_id,
                                 ]);
    $payment->save();

    return redirect()->action('PaymentController@index');
}

Please help on this. Thank you.

3
  • for which line you get the error? Commented Mar 2, 2017 at 7:15
  • @Onix I am not sure. It seems like the input type is string where it should have been array. I guess I need to store array through controller. Commented Mar 2, 2017 at 7:20
  • how you entering amount in input field? Commented Mar 2, 2017 at 7:33

2 Answers 2

4

you are submitting form with array of text fields and select box, try below

public function store(Request $request)
{
    $count = Client::count();
    foreach( $request->client_id as $key=>$val){
    $payment = Payment::create(['amount' => $request->amount[$key],
                                 'client_id'  => $val,
                                 ]);
 }

 return redirect()->action('PaymentController@index');
}
Sign up to request clarification or add additional context in comments.

Comments

0

Ty to create the record like this:

$payment = Payment::create($request->input);

And change you redirect action to this:

View::make('path/to/view/')

or just use just back(); just to test if it works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.