Chonchol Mahmud
Chonchol Mahmud

Reputation: 2735

How to update checkbox field and option field in Laravel 5.2?

I can update text field of a form but i can not update checkbox field,option field and file field in Laravel 5.2. I i going to update then i can see all text value is perfectly shown in update blade view but in option field, checkbox field i see it is default value its don't comes from database. Again if i update with another option then its not saving.What i have tried so far:

My Controller:

public function update(Request $request, $id=0)
{
    //
    $id = $request->input("id");
    $product = Product::find($id);
    $product->product_name = $request->input('product_name');
    $product->product_storage = $request->input('product_storage');
    $product->product_percentage = $request->input('product_percentage');
    $product->can_draw = $request->input('can_draw');
    $product->real_price = $request->input('real_price');
    $product->product_image = $request->input('product_image');

    $product->save();

    $request->session()->flash('alert-info', 'Product Successfully Updated!'); 
    return Redirect::to('admin/product/all');
}

My update.blade.php View:

<div class="form-group">
    <label class="col-md-3 control-label">{{ trans('common.can_be_draw') }}</label>
    <div class="col-md-9">
        <div class="input-icon">
        <i class="fa fa-money" aria-hidden="true"></i>
            <select name="can_draw" class="form-control">
                <option value="{{ $product->can_draw }}">{{ trans('common.open') }}open</option>
                <option value="{{ $product->can_draw }}">{{ trans('common.close') }}close</option>
            </select>
       </div>
    </div>                      
</div>                      
<div class="form-group">
    <label class="col-md-3 control-label">{{ trans('common.real_prize') }}</label>
    <div class="col-md-9">
        <div class="input-icon">

            <input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}"> {{ trans('common.yes') }}yes 
            <input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}" checked="checked"> {{ trans('common.no') }}no
        </div>
    </div>
</div>
<div class="form-group">
    <label for="exampleInputFile" class="col-md-3 control-label">{{ trans('common.product_picture') }}</label>
    <div class="col-md-9">
        <input type="file" id="exampleInputFile" name="product_image" value="{{ $product->product_image }}">
        <small>{{ trans('common.pic_summery') }}</small>
    </div>
</div>

Upvotes: 0

Views: 1675

Answers (3)

Onix
Onix

Reputation: 2179

Just use this:

   <input type="radio" {{$product->can_draw == 'Yes' ? 'checked' : ''}} class="form-control" name="real_price" value="Yes"> {{ trans('common.yes') }}yes 
   <input type="radio" {{$product->can_draw == 'Yes' ? '' : 'checked'}} class="form-control" name="real_price" value="No" > {{ trans('common.no') }}no

EDIT

I have edited the above*

You should change the value to Yes and No and use this to check if the value saved is Yes or No. It does not matter how much is real_price

Upvotes: 1

Dmitriy Doronin
Dmitriy Doronin

Reputation: 778

You just can save option value by requesting from select name. In your example:
select name = "can_draw" Just save value from request:

$product->can_draw = $request->can_draw;

For checkbox same:

$product->real_price = $request->real_price;

Not necessary indicate input() helper in request.

To retrieve all value for option and check box value, please use foreach methods:

@foreach($can_draws as $can_draw)
 <option value="{{ $can_draw->id }}">{{ $can_draw->name}}</option>
@endforeach

Upvotes: 0

Mehdi Daalvand
Mehdi Daalvand

Reputation: 661

your option field have same value ({{ $product->can_draw }})

 <option value="value1"{{( $product->can_draw=='value1'?selected)}}>{{ trans('common.open') }}open</option>
<option value="value2 " {{( $product->can_draw=='value1'?selected)}}>{{ trans('common.close') }}close</option>

and for file field must use file not input:

$product->product_image = $request->file('product_image');

Upvotes: 1

Related Questions