Laravel:CRUD更新表格

时间:2018-10-29 12:18:52

标签: php laravel

事实上,我将希望学习更新有关Laravel的数据。我可以使用记录的信息恢复ID,但是当我更改表单上的几个元素并确认时。

我遇到有关 Illuminate \ Routing \ RouteCollection methodNotAllowed 的问题,错误消息为Auth :: routes(); route :: resource('student','AdminController');

我的水平仍然无法理解问题...我该怎么办?

有关信息,这是我的代码

我有一个名为AdminController的文件

class AdminController extends Controller
{
    public function index()
    {
      $students = Student::paginate(5);
      return view('student.index', compact('students'))
             ->with('i', (request()->input('page',1)-1)*5);
    }

    public function create()
    {
        return view('student.create');

    }

    public function store(Request $request)
    {
        $request->validate([
                'firstname' => 'required',
                'lastname' => 'required'
        ]);

        Student::create($request->all());
        return redirect()->route('student.index')
                    ->with('success', 'save');
    }

    public function edit($id)
    {
        $student = Student::find($id);
        return view('student.edit', compact('student'));

    }

    public function update(Request $request, $id)
    {
        $request->validate([
                'firstname' => 'required',
                'lastname' => 'required'
        ]);

        $student = Student::find($id);
        $student->firstname = $request->get('firstname');
        $student->lastname = $request->get('lastname');
        $student->save();

        return redirect()->route('student.index')
                  ->with('success', 'updated successfully');

    }

}

在View中,我有一个命名为Student的文件夹,其中包含2个文件

index.blade.php

@section('content')
      <div class="px-content">
        <div class="page-header">
            <div class="row">
                <div class="col-md-4 text-xs-center text-md-left text-nowrap">
                    <h1>
                        <i class="px-nav-icon ion-android-apps"></i>List
                    </h1>
                </div>

                <hr class="page-wide-block visible-xs visible-sm">
                <!-- Spacer -->
                <div class="m-b-2 visible-xs visible-sm clearfix"></div>
            </div>
        </div>
        <div class="row">
            <div class="panel">
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table">
                            <a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
                            <thead>
                                <tr>
                                    <th>Firstname</th>
                                    <th>Lastname</th>
                                </tr>
                            </thead>
                            @foreach($students as $student)
                                <tr>
                                    <td> {{$student->firstname}}</td>
                                    <td> {{$student->lastname}} </td>
                                    <td>
                                        <a class="btn btn-sm btn-warning" href="{{route('student.edit',$student->id)}}">Edit</a>
                                    </td>
                                </tr>
                            @endforeach
                        </table>
                    </div>
                </div>
            </div>
        </div>
        {!! $students->links() !!}
    </div>
@endsection

还有 edit.blade.php

@section('content')
    <div class="px-content">
        <div class="page-header">
            <div class="row">
                <div class="col-md-4 text-xs-center text-md-left text-nowrap">
                    <h1>
                        <i class="px-nav-icon ion-android-apps"></i>Edit {{$student->id}}
                    </h1>
                </div>

                <hr class="page-wide-block visible-xs visible-sm">
                <!-- Spacer -->
                <div class="m-b-2 visible-xs visible-sm clearfix"></div>
            </div>
        </div>
        <div class="row">
            <div class="panel">
                <div class="panel-body">
                    <div class="table-responsive">
                        <form class="panel-body" action="/student/edit/{{$student->id}}" method="POST">
                            @csrf
                            <fieldset class="form-group">
                            <label for="form-group-input-1">Firstname</label>
                                <input type="text" name="firstname" class="form-control" id="form-group-input-1" value="{{$student->firstname}}">
                            </fieldset>
                            <fieldset class="form-group">
                                <label for="form-group-input-1">Lastname</label>
                                <input type="text" name="valeur" class="form-control" id="form-group-input-1" value="{{$lastname->valeur}}">
                            </fieldset>
                            <fieldset class="form-group">
                                <label for="form-group-input-1">Page</label>
                                <input type="text" name="page" class="form-control" id="form-group-input-1" value="{{$tariff->page}}">
                            </fieldset>
                            <button type="submit" class="btn btn-primary pull-right">MAJ</button>
                        </form>
                    </div>  
                </div>
            </div>
        </div>
    </div>
@endsection

关于文件夹路由,我有

Auth::routes();
route::resource('student','AdminController');

请问您有个主意吗?因为我不明白我的问题...

5 个答案:

答案 0 :(得分:1)

由于HTML表单无法发出PUTPATCHDELETE请求,因此您需要添加hidden _method字段来欺骗这些HTTP动词。 @method Blade指令可以为您创建此字段:

<form class="panel-body" action="/student/edit/{{$student->id}}" method="POST">
     @method('PUT')
 </form>

答案 1 :(得分:1)

在表单内部添加此行

{{ method_field('PUT') }}

答案 2 :(得分:0)

使用用于路由的资源,您的更新路由将具有方法PUT|PATCH,因此为了能够在put内以您的表单发出edit.blade.php请求,您会丢失

@method('PUT')

请记住,表单上的method标签应保留POST,这是一个附加字段。

请大家在回答之前先阅读一些文档 https://laravel.com/docs/master/routing#form-method-spoofing

答案 3 :(得分:0)

在您的edit.blade.php文件中,您将POST方法用于表单标签。您应该将其更改为PUT

<form class="panel-body" action="/student/edit/{{$student->id}}" method="POST">

将其更改为

<form class="panel-body" action="/student/edit/{{$student->id}}" method="PUT">

因为route :: resource()会生成PUT方法路由进行更新。

答案 4 :(得分:-2)

只需在您的<form>之后在刀片中添加以下代码

<input name="_method" type="hidden" value="PATCH">

,并使用Route::PATCH('/update/{id}','YourController@update');

更改路线