Laravel 5.2无法更新记录

时间:2016-02-23 04:01:22

标签: laravel routes laravel-5.2

我似乎无法更新我的记录。

我的控制器

public function add()
{
    return view('cars.add');
}

public function edit($id)
{
    $car = Cars::whereId($id)->firstOrFail();
    return view('cars.edit', compact('car'));
}

public function store(CarFormRequest $request)
{
    $car = new Cars(array(
        'name' => $request->get('name'),
        'color_id' => $request->get('color')
    ));

    $car->save();
    $car->position_id = $car->id;
    $car->save();

    session()->flash('status', 'Successfully Added a Car!');
    return view('cars.add');
}

public function update($id, CarFormRequest $request)
{
    $car = car::whereId($id)->firstOrFail();
    $car->name = $request->get('name');
    $car->color_id = $request->get('color');
    if($request->get('status') != null) {
        $car->status = 0;
    } else {
        $car->status = 1;
    }
    $car->save();
    return redirect(action('CarController@edit', $car->id))->with('status', 'The ticket '.$id.' has been updated!');

}

我的路线:

Route::get('/', 'PagesController@home');
Route::get('/about', 'PagesController@about');
Route::get('/contact', 'PagesController@contact');
Route::get('/cars', 'CarsController@index');
Route::get('/cars/edit/{id?}', 'CarsController@edit');
Route::post('/cars/edit/{id?}', 'CarsController@update');
Route::get('/cars/add', 'CarsController@add');
Route::post('/cars/add', 'CarsController@store');

这是我的观点:

<div class="container col-md-8 col-md-offset-2">
<div class="well well bs-component">
    <form class="form-horizontal" method="post">
        <input type="hidden" name="_token" value="{!! csrf_token() !!}">
        <input type="text" id="color_id" name="color_id" value="{!! $car->color_id !!}">
        <fieldset>
            <legend>Edit Car Information</legend>
            <div class="form-group">
                <label for="title" class="col-lg-2 control-label">Car Name</label>
                <div class="col-lg-10">
                    <input type="text" value="{{ $car->name }}" class="form-control" id="name" placeholder="Car Name">
                </div>
            </div>
            <div class="form-group">
                <label for="title" class="col-lg-2 control-label">Car Color</label>
                <div class="col-lg-10">
                    <div class="btn-group" data-toggle="buttons">
                        <label id="opt1" class="btn btn-primary">
                            <input type="radio" name="color" id="option1" autocomplete="off"> Red
                        </label>
                        <label id="opt2" class="btn btn-primary">
                            <input type="radio" name="color" id="option2" autocomplete="off"> Blue
                        </label>
                        <label id="opt3"  class="btn btn-primary">
                            <input type="radio" name="color" id="option3" autocomplete="off"> Yellow
                        </label>
                        <label id="opt4" class="btn btn-primary">
                            <input type="radio" name="color" id="option4" autocomplete="off"> Green
                        </label>
                        <label id="opt5" class="btn btn-primary">
                            <input type="radio" name="color" id="option5" autocomplete="off"> Black
                        </label>
                        <label id="opt6" class="btn btn-primary">
                            <input type="radio" name="color" id="option6" autocomplete="off"> White
                        </label>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-10 col-lg-offset-2">
                    <button class="btn btn-default">Cancel</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </fieldset>
    </form>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

whereIn中的$id变量必须是数组,您还需要指定数据库列。这应该是 -

public function edit($id)
{
    $car = Cars::whereId('id', [$id])->firstOrFail();
    return view('cars.edit', compact('car'));
}

更改所有出现的

$car = car::whereId($id)->firstOrFail();