为什么不更新Laravel 5.6中的表列值?

时间:2018-10-25 05:33:14

标签: php mysql laravel-5

在laravel 5.6应用程序中,我的表名称为车辆,然后我需要在VehicleController更新函数中更新一些表值,

public function update(Request $request, $id)
    {

        $vehicle = Vehicle::find($id);

        $vehicle->provincename = $request->input('provincename');
        $vehicle->districtname = $request->input('districtname');
        $vehicle->townname = $request->input('townname');
        $vehicle->brandname = $request->input('brandname');
        $vehicle->modelname = $request->input('modelname');
        $vehicle->modelyear = $request->input('year');
        $vehicle->condition = $request->input('condition');
        $vehicle->milage = $request->input('milage');
        $vehicle->detail = $request->input('data');
        $vehicle->price = $request->input('price');
        $vehicle->telephone = $request->input('telephone');
        $vehicle->categoryname =  $request->input('categoryname');
        $vehicle->transmission = $request->input('transmission');
        $vehicle->fueltype = $request->input('fueltype');
        $vehicle->enginecapacity = $request->input('enginecapacity');
        $vehicle->user_id = Auth::user()->id;

        $vehicle->save();

和编辑表单操作是

<form  method="post"  action="{{ route('vehicles.edit', [$vehicles->id])  }}" enctype="multipart/form-data">

更新路径为

Route::post('myads/{id}', [
    'uses' => '\App\Http\Controllers\VehicleController@update',
])->name('vehicles.edit');

和控制器编辑功能

 public function edit($id)
    {
       $vehicles = Vehicle::findOrFail($id);
}

编辑路径为

Route::get('myads/{id}/edit', [
    'uses' => '\App\Http\Controllers\VehicleController@edit',
    'as'=> 'vehicles.edit'
]);

但是当我单击更新按钮时,它没有更新值。这里没有发生任何错误,仅重定向回编辑表单。如何解决这个问题?

车辆型号

class Vehicle extends Model
{
    use Searchable;
     protected $guarded = [];

    public function searchableAs()
    {
        return 'categoryname';
    }

     public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function uploads()
    {
        return $this->hasMany(Upload::class);
    }

     public function cars()
    {
        return $this->hasMany(Car::class);
    }

    public function vans()
    {
        return $this->hasMany(Car::class);
    }

     public function scopePersonal($query)
{
     return $query->where('user_id', Auth::user()->id);

}
}

编辑表单是

<form  method="post"  action="{{ route('vehicles.edit', [$vehicles->id])  }}" enctype="multipart/form-data">
                {{csrf_field()}}
                <div class="form-group{{ $errors->has('provincename') ? ' has-error' : '' }}">
            <label for="exampleFormControlSelect1">Province</label>
        <select name="provincename" id="provincename" class="form-control input dynamic" data-dependent="districtname" >
            <option value="{{$vehicles->provincename}}">{!! $vehicles->provincename !!}</option>
            @foreach($town_list as $town)

            <option value="{{$town->provincename}}">{{$town->provincename}}</option>
            @endforeach
        </select>
         @if ($errors->has('provincename'))
                    <span class="help-block">{{ $errors->first('provincename') }}</span>
                @endif
        </div>

        <div class="form-group{{ $errors->has('districtname') ? ' has-error' : '' }}">
            <label for="exampleFormControlSelect1">District</label>
           <select name="districtname" id="districtname" class="form-control input dynamic" data-dependent="townname" >
            <option value="{{$vehicles->districtname}}">{!! $vehicles->districtname !!}</option>



        </select>
         @if ($errors->has('districtname'))
                    <span class="help-block">{{ $errors->first('districtname') }}</span>
                @endif
        </div>

        <div class="form-group{{ $errors->has('townname') ? ' has-error' : '' }}">
            <label for="exampleFormControlSelect1">Town</label>
        <select name="townname" id="townname" class="form-control input">
            <option value="{{$vehicles->townname}}">{!! $vehicles->townname !!}</option>

        </select>
        @if ($errors->has('townname'))
                    <span class="help-block">{{ $errors->first('townname') }}</span>
                @endif

        </div>

         <!--hidden select box-->

            <div class="form-group" style="display: none;">
            <label for="exampleFormControlSelect1">Vehicle Category</label>
        <select name="categoryname" id="categoryname" class="form-control input dynamic" data-dependent="brandname" >

            @foreach($model_list as $model) 
            <option value="{{$vehicles->categoryname}}">{{$vehicles->categoryname}}</option>
            @endforeach


        </select>
    </div>



     <div class="form-group{{ $errors->has('brandname') ? ' has-error' : '' }}">
            <label for="exampleFormControlSelect1">Brand</label>

           <select name="brandname" id="brandname" class="form-control input dynamic" data-dependent="modelname" >
            <option value="{{$vehicles->brandname}}">{!! $vehicles->brandname !!}</option>

        </select>
        @if ($errors->has('brandname'))
                    <span class="help-block">{{ $errors->first('brandname') }}</span>
                @endif
        </div>


        <div class="form-group{{ $errors->has('modelname') ? ' has-error' : '' }}">
            <label for="exampleFormControlSelect1">Model</label>
        <select name="modelname" id="modelname" class="form-control input">
            <option value="{{$vehicles->modelname}}">{!! $vehicles->modelname !!}</option>

        </select>
        @if ($errors->has('modelname'))
                    <span class="help-block">{{ $errors->first('modelname') }}</span>
                @endif

        </div>

        <div class="form-group{{ $errors->has('year') ? ' has-error' : '' }}">
        <label for="formGroupExampleInput">Model Year</label>
        <input type="text" class="form-control" id="year" placeholder="Year" name="year" value="{!! $vehicles->modelyear ?: '' !!}">
        @if ($errors->has('year'))
                    <span class="help-block">{{ $errors->first('year') }}</span>
                @endif
        </div>

        <div class="form-group{{ $errors->has('condition') ? ' has-error' : '' }}">
        <label for="formGroupExampleInput">Condition</label>


        <label class="radio-inline"><input type="radio" name="condition" value="used" @if($vehicles->condition == 'used') checked @endif>Used</label>
<label class="radio-inline"><input type="radio" name="condition" value="recondition" @if($vehicles->condition == 'recondition') checked @endif>Recondition</label>
<label class="radio-inline"><input type="radio" name="condition" value="new" @if($vehicles->condition == 'new') checked @endif> New</label>
  @if ($errors->has('condition'))
                    <span class="help-block">{{ $errors->first('condition') }}</span>
                @endif
        </div>

        <div class="form-group{{ $errors->has('milage') ? ' has-error' : '' }}">
        <label for="formGroupExampleInput">Milage</label>
        <input type="text" class="form-control" id="milage" placeholder="Milage" name="milage" value="{!! $vehicles->milage ?: '' !!}">
        @if ($errors->has('milage'))
                    <span class="help-block">{{ $errors->first('milage') }}</span>
                @endif
        </div>

        <div class="form-group{{ $errors->has('transmission') ? ' has-error' : '' }}">
        <label for="exampleFormControlSelect1">Transmission</label>
        <select class="form-control" id="transmission" name="transmission">

            <option value="{!! $vehicles->transmission  !!}">{!! $vehicles->transmission  !!}</option>

        <option value="Manual">Manual</option>
        <option value="Auto">Auto</option>
        <option value="Hybrid">Hybrid</option>
        <option value="Electric">Electric</option>
        <option value="Codak">codak</option>
        </select>
        @if ($errors->has('transmission'))
                    <span class="help-block">{{ $errors->first('transmission') }}</span>
                @endif
        </div> 

        <div class="form-group{{ $errors->has('fueltype') ? ' has-error' : '' }}">
        <label for="exampleFormControlSelect1">Fuel Type</label>
        <select class="form-control" id="fueltype" name="fueltype">

            <option value="{!! $vehicles->fueltype  !!}">{!! $vehicles->fueltype  !!}</option>

        <option value="Petrol">Petrol</option>
        <option value="Diesel">Diesel</option>
        <option value="Hybrid">Hybrid</option>
        <option value="Electric">Electric</option>

        </select>
         @if ($errors->has('fueltype'))
                    <span class="help-block">{{ $errors->first('fueltype') }}</span>
                @endif
        </div> 

        <div class="form-group{{ $errors->has('enginecapacity') ? ' has-error' : '' }}">
        <label for="formGroupExampleInput">Engine capacity</label>

        <input type="text" class="form-control" id="enginecapacity" placeholder="Engine capacity" name="enginecapacity"  value="{!! $vehicles->enginecapacity ?: '' !!}" >
         @if ($errors->has('enginecapacity'))
                    <span class="help-block">{{ $errors->first('enginecapacity') }}</span>
                @endif

        </div>


        <div class="form-group{{ $errors->has('data') ? ' has-error' : '' }}">
        <label for="comment">More Details</label>
        <textarea class="form-control" rows="5" id="data" name="data" rows="10" cols="10">{!! trim($vehicles->detail) !!}</textarea>
        @if ($errors->has('data'))
                    <span class="help-block">{{ $errors->first('data') }}</span>
                @endif
        </div >

        <div class="form-group{{ $errors->has('price') ? ' has-error' : '' }}">
        <label for="formGroupExampleInput">Price</label>
        <input type="text" class="form-control" id="price" placeholder="Price" name="price" value="{!! $vehicles->price ?: '' !!}">
         @if ($errors->has('price'))
                    <span class="help-block">{{ $errors->first('price') }}</span>
                @endif
        </div>

        <div class="form-group{{ $errors->has('telephone') ? ' has-error' : '' }}">
        <label for="formGroupExampleInput">Telephone</label>
        <input type="text" class="form-control" id="telephone" placeholder="Telephone" name="telephone" value="{!! $vehicles->telephone ?: '' !!}" >
         @if ($errors->has('telephone'))
                    <span class="help-block">{{ $errors->first('telephone') }}</span>
                @endif
        </div>



 <!-- <button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>
        </div> -->



        @if( $vehicles->uploads->count() > 0 )

                            @php
                                $upload = $vehicles->uploads->sortByDesc('id')->first();
                            @endphp
                           <!--  <img id="preview" src="/images/{{ $upload->resized_name }}"> -->

                   <!--edit/delete buttons-->
@foreach( $vehicles-> uploads as $upload)

                    <img id="preview"
                         src="{{asset((isset($upload) && $upload->resized_name!='')?'images/'.$upload->resized_name:'images/noimage.png')}}"
                         height="200px" width="200px"/>
                    <input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">
                    <br/>
                    <!-- <a href="javascript:changeProfile();">Add Image</a> | -->
                    <!-- <a style="color: red" href="javascript:removeImage()">Delete</a>
                    <input type="hidden" style="display: none" value="0" name="remove" id="remove"> -->
               <a href="/myads/{{$upload->id}}/editimage">Edit Image</a>|

               <a class="button is-outlined" href="/myads/{{$upload->id}}/delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a></td>






                   <hr>
                   @endforeach
                    @endif

              <button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>
        </div>

3 个答案:

答案 0 :(得分:0)

尝试这种方式

并在下面删除或注释此行

//$vehicle->save();
$vehicle = array('provincename'=>$request->input('provincename'),
'districtname' => $request->input('districtname'),
'townname' => $request->input('townname'),
'brandname' => $request->input('brandname'),
'modelname' => $request->input('modelname'),
'modelyear' => $request->input('year'),
'condition' => $request->input('condition'),
'milage' => $request->input('milage'),
'detail' => $request->input('data'),
'price' => $request->input('price'),
'telephone' => $request->input('telephone'),
'categoryname' =>  $request->input('categoryname'),
'transmission' => $request->input('transmission'),
'fueltype' => $request->input('fueltype'),
'enginecapacity' => $request->input('enginecapacity'),
'user_id' => Auth::user()->id);
Vehicle::where('id', $id)->update($vehicle);

答案 1 :(得分:0)

会有多种原因

1. You are not passing the csrf token with form request.
2. There will be one or more input value missing and you have not show the error message in the validation.
3. Vehicle id not exist.
etc. 

答案 2 :(得分:0)

您可以使用数组更新记录,并确保在提交数据时传递csrf令牌。将user_id列添加到数组。

 DB::table('vehicles')
                ->where('id', $vehicle)
                ->update(['provincename ' => $request->input('provincename'),
                          'districtname'=>$request->input('districtname'),
                          'townname' =>input('townname'), 'user_id'=>Auth::user()->id ]);