Laravel:CRUD更新表单

n7taea2i  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(94)

事实上,我希望学会在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');

    }

}

字符串
在视图中,我有一个名为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');


你有办法吗?因为我不明白我的问题...

6g8kf2rb

6g8kf2rb1#

在表单中任意位置添加这一行

{{ method_field('PUT') }}

字符串

mznpcxlj

mznpcxlj2#

只需在刀片式服务器中的<form>后添加以下代码

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

字符串
并使用Route::PATCH('/update/{id}','YourController@update');更改您的路线

bksxznpy

bksxznpy3#

使用路由的资源,您的更新路由将具有方法PUT|PATCH,因此为了能够在edit.blade.php内的表单中发出put请求,您缺少
@method('PUT')
请记住,表单上的method标记应保留为POST,这是一个附加字段。
请大家在回答之前阅读一些文档https://laravel.com/docs/master/routing#form-method-spoofing

jhkqcmku

jhkqcmku4#

在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方法的路由进行更新。

相关问题