【问题标题】:Laravel 5.2 cannot update recordLaravel 5.2 无法更新记录
【发布时间】:2016-02-23 04:01:21
【问题描述】:

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

我的控制器

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>

【问题讨论】:

  • 你为什么在你的路由中使用{id?}而不是{id}
  • 你能分享一下你的blade/html表单结构吗?
  • 我在教程中看到了他们使用的东西,有什么区别吗?
  • dd($car) 得到什么?
  • @JilsonThomas 在什么控制器上? dd 是什么?

标签: laravel routes laravel-5.2


【解决方案1】:

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

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

改变所有出现的

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2023-03-28
    • 2021-05-09
    • 1970-01-01
    • 2018-10-02
    相关资源
    最近更新 更多