【问题标题】:Why did not update table columns values in Laravel 5.6?为什么在 Laravel 5.6 中没有更新表格列的值?
【发布时间】:2018-10-25 14:44:05
【问题描述】:

在 laravel 5.6 应用程序中,我的表名为 vehicles,然后我需要更新 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>

【问题讨论】:

  • 更新记录时为什么使用$vehicle-&gt;save();而不是update
  • 那我该怎么用呢
  • 短代码是$vehicle = Vehicle::find($id); $vehicle-&gt;fill($request-&gt;all()); $vehicle-&gt;update()。并且您的类Vehicle 必须在$fillable 属性中声明所有列名。喜欢protected $fillable = ['provincename', 'districtname', 'townname'];(请把所有的列名都写在这个数组里,我不能给你们都写)。
  • 因为这个])-&gt;name('vehicles.edit');,你的html表单动作可能是这样的action="{{ route('vehicles.edit.edit', [$vehicles-&gt;id]) }}"(再添加一个edit。)或者你可以这样写action="{{ action('\App\Http\Controllers\VehicleController@update')}}"
  • @NgocNam 当我使用上述操作时收到此错误缺少 [Route: vehicle.edit] [URI: myads/{id}] 所需的参数。

标签: php mysql laravel-5


【解决方案1】:

会有多种原因

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. 

【讨论】:

  • 我首先检查了我已经解决的所有问题。但仍然没有结果。你能告诉我找到车辆ID的方法来检查它是否传递给控制器​​吗?
  • 您可以通过隐藏字段在表单中传递车辆id,并在表单中使用$request->input('vehicle_id'); 获取此id;
  • 不,实际上在控制器中它很好地传递了车辆 ID
【解决方案2】:

试试这个方法

并在下面删除或评论此行

//$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);

【讨论】:

  • 你能检查一下我的编辑表格,有没有错误?
  • 我发现了问题。我的更新没问题。这是工作。但通过控制器验证,它不会更新。未经验证它正在工作。如何解决这个问题?这有什么问题?
【解决方案3】:

您可以使用数组来更新您的记录,并确保在提交数据时传递 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 ]);

【讨论】:

  • 这样怎么更新我的$vehicle->user_id = Auth::user()->id;
  • 编辑了答案。您正在根据车辆 ID 进行更新,ryt?
  • 你能检查一下我的编辑表格,有没有错误?
  • 你检查过所有回复都来自表单 submit() 吗?
  • 我如何检查这个?
猜你喜欢
  • 2019-03-29
  • 2018-08-21
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多