简体   繁体   中英

I am beginner trying to update status on view table laravel but i am getting controller error that (Attempt to read property "status" on null)

invoices/index.blade.php

    <td>

                              <form action="{{url('/invoice_status_upadated')}}" method="POST">
                                
                                {{ csrf_field() }}
                                <div class="input-group mb-3">
                              <select class="form-select" aria-label="Default select example">
                                <option value="0" {{$inv->status == 0 ? 'selected':''}}>Pending </option>
                                <option value="1" {{$inv->status == 1 ? 'selected':''}}>In Process </option>
                                <option value="2" {{$inv->status == 2 ? 'selected':''}}>Completed </option>
                                <option value="3" {{$inv->status == 3 ? 'selected':''}}>Cancelled </option>
                                

                                  <?php 
                                  if ($inv->status == 0){
                                    echo "selected";
                                  }
                                    ?>
                                    
          
                                <?php 
                                if ($inv->status == 1){
          
                                  echo "selected";
                                }
                                
                                  ?>
                                 <?php 
                                 if ($inv->status == 2){
           
                                   echo "selected";
                                 }
                                 
                                   ?>                                         
                                <?php 
                                if ($inv->status == 3){
          
                                  echo "selected";
                                }
                                
                                  ?> 
                            
    
                            </select>
                            <button type="submit"  class="btn btn-outline-success">Update</button>

                            </div>
                              </form>

                            
                            </td> 

/TemplateContorller

public function invoice_status_upadated(Request $request){ 
    $data= Invoice::find($request->status);
    $data->status=$request->$data->status;
    $data->save();
    return redirect('invoices');
}

web.php

Route::post('/invoice_status_upadated', 'App\Http\Controllers\TemplateController@invoice_status_upadated')->name('invoice_status_upadated');

View

表视图

error

在此处输入图像描述

Instead of doing $data->status=$request->$data->status; do $data->status = $request->status;

$request does not have the data from the model, which is in the $data variable.

Try this:

<select class="form-select" aria-label="Default select example" name="status">

And also in your controller you have to change:-

$data = Invoice::find($invoice_id);
$data->status = $request->status;
$data->save();

Hope this will work for you.

Hi first of all you need to async you select tag with name tag and set it to status for example

<select class="form-select" aria-label="Default select example" name="status">
<input type="hidden" value="{{ $inv->id }}" name="inv_id"/>

then in your controller first of all you need to validate your request incomping data for mor information please see this in laravel documentations

after that you need to change your code like this

$data= Invoice::find($request->inv_id);
$data->status= $data->status;

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