Laravel 5 Update existing in database

I'm trying to update an existing row in my table using a form in a bootstrap modal. But nothing changes when I attempt it. I have an html table with a list of cars and in each row a button "Change Status" that opens the bootstrap modal with the id="myModal_{{ $car->id }}" and with the form inside it to edit the status of a car in repair.

Here's my modal:

<!-- Modal -->
<div class="modal fade" id="myModal_{{ $car->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Breyta stöðu bíls</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <h3>Breyta stöðu fyrir {{ $car->LicencePlate }}</h3>
        </div>
            {!! Form::open(['url' => 'create']) !!}

              <div class="form-group">
                {!! Form::label('Status', 'Status: ') !!}
                {!! Form::select('Status', [
                                  null => ' -- Veldu Stöðu bíls -- ',
                                  'Í Biðröð' => 'Í Biðröð',
                                  'Í Viðgerð' => 'Í Viðgerð',
                                  'Tilbúinn' => 'Tilbúinn'
                                  ], null, ['class' => 'form-control']) !!}
              </div>
              <div class="form-group">

              </div>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        {!! Form::submit('Skrá stöðu', ['class' => 'btn btn-primary']) !!}
      </div>
      {!! Form::close() !!}
    </div>
  </div>
</div>

Update method in CarController:

public function CarEdit($id, Requests\CreateCarsRequest $request)
{
  $edit = Car::update($request->all());
  return redirect::back();
}

My Route to that method:

Route::post('/dashboard', 'CarController@CarEdit');

Upvotes: 0

Views: 239

Answers (2)

I was able to fix this with the help of my colleagues. What I did was I created a new requet called UpdateCarRequest where the LicencePlate field was not unique. And edited my method like so:

public function CarEdit(Requests\UpdateCarRequest $request)
{
  $edit = Car::where('LicencePlate', '=', $request->LicencePlate)->first();
  $edit->update($request->all());
  return redirect('/dashboard');
}

I also added a hidden field in the form with the value $car->LicencePlate.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

You should do this:

['action' => 'CarController@CarEdit']

Also, you're using mass assignment, so you must fill $fillable array in your Car model to make it work: https://laravel.com/docs/5.1/eloquent#mass-assignment

Upvotes: 1

Related Questions