BStud BStud - 2 months ago 12
PHP Question

Update database of button click in laravel form

I have this on my view

{{ Form::open() }}
@if($order->paid == 0)
<button type="submit" class="btn btn-primary" value="1">Mark Order as Paid</a>
@else
<button type="submit" class="btn btn-primary" value="0">Mark Order as Unpaid</a>
@endif
{{ Form::close() }}


And this in my controller

public function ordersPaidSubmit($orderId) {
$order = Order::where('order_id', $orderId)->first();
if (!$order) {
App::abort(404);
}

$paid = Input::get('paid');
$order->save();

return Redirect::to('/admin/orders');
}


Is it possible to give value to buttons
0
or
1
like this and on click to update database column? Currently doesn't update but how can be make to update?

Answer

You can put one hidden field in each button like this:

{{ Form::open() }}
@if($order->paid == 0)
    {{ Form::hidden('paid', 1, ['id' => 'paid']) }}
    <button type="submit" class="btn btn-primary" value="1">Mark Order as Paid</a>
@else
    {{ Form::hidden('paid', 0, ['id' => 'paid']) }}
    <button type="submit" class="btn btn-primary" value="0">Mark Order as Unpaid</a>
@endif
{{ Form::close() }}

So hidden field will looks like

<input id="paid" name="paid" type="hidden" value="1">
and
<input id="paid" name="paid" type="hidden" value="0">

I'm sure that you want to use $order not $paid in your controller. So change this line

$paid = Input::get('paid');

whit

$order->paid = Input::get('paid');

And your form buttons will work.