在laravel中更新表(crud)时出错

时间:2019-06-07 05:33:39

标签: html css laravel crud

当我准备编辑表时,出现以下错误:

  

“此路由不支持POST方法。受支持的方法:   GET,HEAD。”

<?php

Route::get('/crear',[

    'uses'=>'CarController@mostrar',

    'as'=>'cars.create'

]   

);

Route::post('/crear',[

    'uses'=>'CarController@crear',

    'as'=>'cars.crear'

]);

Route::get('/', 'CarController@casa' );

Route::post('cars/{id?}/editar', 'CarController@edit')->name('editarcar');

Route::post('cars/{id?}/editar', 'CarController@update');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('cars', 'CarController');

可见

@extends('layouts.app')

@section('title', 'Contact')

@section('content')

    <div class="container col-md-8 col-md-offset-2">

        <div class="well well bs-component">

            <form class="form-horizontal" method="post">

                @foreach ($errors->all() as $error)

                    <div class="alert alert-danger">{{ $error }}</div>

                @endforeach

                @if(session('status'))

                    <div class="alert alert-success">

                        {{ session('status') }}

                    </div>

                @endif

                {!! csrf_field() !!}

                <fieldset>

                    <legend>Editar </legend>

                    <div class="form-group">

                        <label for="patente" class="col-lg-label">patente</label>

                        <div class="col-lg-10">

                            <input type="text" name="patente"size="6" maxlength="6" class="form-control"required>

                        </div>

                    </div>

                    <div class="form-group">

                        <label for="marca" class="col-lg-label">marca</label>

                        <div class="col-lg-10">

                            <input type="text" name="marca" class="form-control" required> 

                        </div>

                    </div>

                    <div class="form-group">

                        <label for="modelo" class="col-lg-label">modelo</label>

                        <div class="col-lg-10">

                            <input type="text" name="modelo" class="form-control" required>

                        </div>

                    </div>

                    <div class="form-group">

                        <label for="color" class="col-lg-label">color</label>

                        <div class="col-lg-10">

                            <input type="text" name="color" class="form-control" required>

                        </div>

                    </div>

                    <div class="form-group">

                        <div class="col-lg-10 col-lg-offset-2">

                            <button class="btn btn-default">Cancelar</button>

                            <button type="submit" class="ntm btn-primary">Actualizar</button>

                        </div>

                    </div>

                </fieldset>

            </form>

        </div>

    </div>

@endsection

控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


namespace App\Http\Controllers;

use App\Car;

use Illuminate\Http\Request;


    public function edit($id)
    {
        $car = car::whereid($id)->firstOrFail();

        return view('edit', compact('cars'));

    }

    public function update(Request $request, $id)
    {

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

        $car->patente = $request->post('patente');

        $car->marca = $request->post('marca');

        $car->modelo = $request->post('modelo');

        $car->color = $request->post('color');

        $car->save();

        return redirect(action('CarsController@edit', $car->id))->with('status', 'El car ' . $id . ' ha sido actualizado');

    }

1 个答案:

答案 0 :(得分:0)

更新控制器>使用正确的变量compact来编辑动作。

public function edit($id)
{
    $car = car::whereid($id)->firstOrFail();

    return view('edit', compact('car'));

}

添加一个{{ route("cars.update", ['car' => $car->id]) }}之类的表单操作,以便您的视图如下:

@extends('layouts.app')

@section('title', 'Contact')

@section('content')

    <div class="container col-md-8 col-md-offset-2">

        <div class="well well bs-component">

            <form class="form-horizontal" action="{{ route("cars.update") }}" method="post">

                @foreach ($errors->all() as $error)

                    <div class="alert alert-danger">{{ $error }}</div>

                @endforeach

                @if(session('status'))

                    <div class="alert alert-success">

                        {{ session('status') }}

                    </div>

                @endif

                {!! csrf_field() !!}

                <fieldset>

                    <legend>Editar </legend>

                    <div class="form-group">

                        <label for="patente" class="col-lg-label">patente</label>

                        <div class="col-lg-10">

                            <input type="text" name="patente"size="6" maxlength="6" class="form-control"required>

                        </div>

                    </div>

                    <div class="form-group">

                        <label for="marca" class="col-lg-label">marca</label>

                        <div class="col-lg-10">

                            <input type="text" name="marca" class="form-control" required> 

                        </div>

                    </div>

                    <div class="form-group">

                        <label for="modelo" class="col-lg-label">modelo</label>

                        <div class="col-lg-10">

                            <input type="text" name="modelo" class="form-control" required>

                        </div>

                    </div>

                    <div class="form-group">

                        <label for="color" class="col-lg-label">color</label>

                        <div class="col-lg-10">

                            <input type="text" name="color" class="form-control" required>

                        </div>

                    </div>

                    <div class="form-group">

                        <div class="col-lg-10 col-lg-offset-2">

                            <button class="btn btn-default">Cancelar</button>

                            <button type="submit" class="ntm btn-primary">Actualizar</button>

                        </div>

                    </div>

                </fieldset>

            </form>

        </div>

    </div>

@endsection