0
votes

in laravel 5.6 app I have table name as vehicles, then I need update some of table values in VehicleController update function as this,

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();

and edit form action is,

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

and update route is,

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

and controller edit function,

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

and edit route is,

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

but when I click update button it did not update values. no any error occurred here only redirect back to edit form. how can fix this problem?

vehicle model

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

}
}

edit form is,

<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
when update a record than why use $vehicle->save(); instead of updateBhargav Chudasama
then how can I use itbanda
Shorter code is $vehicle = Vehicle::find($id); $vehicle->fill($request->all()); $vehicle->update(). And your class Vehicle must declare all column names in $fillable attribute. Like protected $fillable = ['provincename', 'districtname', 'townname']; (please write all column names in this array, I cannot write for you all).Ngoc Nam
And because of this ])->name('vehicles.edit');, your html form action may be like this action="{{ route('vehicles.edit.edit', [$vehicles->id]) }}" (add one more edit.) Or your can write like this action="{{ action('\App\Http\Controllers\VehicleController@update')}}"Ngoc Nam
@NgocNam when I use above action got this error Missing required parameters for [Route: vehicles.edit] [URI: myads/{id}].banda

3 Answers

0
votes

There will be mutiple reasons

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. 
0
votes

try this way

and remove or comment this lines below

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

You can use array to update your record and also make sure to pass csrf token when submitting a data. Add user_id column to the array.

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