2
votes

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.

2
for which line you get the error?Onix
@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.Ahsan
how you entering amount in input field?Ramzan Mahmood

2 Answers

4
votes

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');
}
0
votes

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