缺少 [Route: one_one.update] 的必需参数 [URI: one_one/{one_one}] [缺少参数: one_one]

huangapple go评论66阅读模式
英文:

Missing required parameter for [Route: one_one.update] [URI: one_one/{one_one}] [Missing parameter: one_one]

问题

我在{{ route('one_one.update',$student->id) }}处遇到了错误,因此无法使用编辑进行CRUD操作,因为出现了这个错误。

路由(我正在使用资源控制器,所以我的路由是这样的)

Route::resource('/one_one', StudentController::class);

edit.blade.php

<form method="POST" action="{{ route('one_one.update',$student->id) }}" enctype="multipart/form-data">
        @csrf
        @method('PUT')
        Name <input type="text" name="name" value="{{ $student->name }}"><br><br>
        Contact <input type="text" name="contact" value="{{ $student->contact }}"><br><br>
        Email <input type="text" name="email" value="{{ $student->email }}"><br><br>
    <h3>Student Details</h3>
        Alternative contact <input type="text" name="alter_contact" value="{{ $student->studentdetail->alter_contact }}"><br><br>
        Address <input type="text" name="address" value="{{ $student->studentdetail->address }}"><br><br>
        Course <input type="text" name="course" value="{{ $student->studentdetail->course }}"><br><br>
        <button type="submit">Submit</button>
    </form>

index.balde.php

<form action="{{ route('one_one.edit',$student->id) }}"><button type="submit">Edit</button></form>
                <form action="{{ route('one_one.destroy',$student->id) }}" method="POST">
                    @csrf
                    @method('DELETE')
                    <button type="submit">Delete</button></form>

控制器

public function edit(Student $student)
    {
        return view('one_one.edit',compact('student'));
    }
    public function update(Request $request, Student $student)
    {
        $student->update([
            'name'=>$request->name,
            'contact'=>$request->contact,
            'email'=>$request->email,
        ]);

        $student->studentdetail()->update([
            'alter_contact'=>$request->alter_contact,
            'address'=>$request->address,
            'course'=>$request->course,
        ]);
        return redirect()->route('one_one.index')->with('success','Data updated successfully');
    }

我也尝试将$student->id作为数组传递,但仍然不起作用。

英文:

I am getting error at {{ route('one_one.update',$student->id) }} and I am unable to use edit for CRUD operation because of this error.

Route (I am using resource controller so my route is this)

Route::resource(&#39;/one_one&#39;,StudentController::class);

edit.blade.php

&lt;form method=&quot;POST&quot; action=&quot;{{ route(&#39;one_one.update&#39;,$student-&gt;id) }}&quot; enctype=&quot;multipart/form-data&quot;&gt;
        @csrf
        @method(&#39;PUT&#39;)
        Name &lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;{{ $student-&gt;name }}&quot;&gt;&lt;br&gt;&lt;br&gt;
        Contact &lt;input type=&quot;text&quot; name=&quot;contact&quot; value=&quot;{{ $student-&gt;contact }}&quot;&gt;&lt;br&gt;&lt;br&gt;
        Email &lt;input type=&quot;text&quot; name=&quot;email&quot; value=&quot;{{ $student-&gt;email }}&quot;&gt;&lt;br&gt;&lt;br&gt;
    &lt;h3&gt;Student Details&lt;/h3&gt;
        Alternative contact &lt;input type=&quot;text&quot; name=&quot;alter_contact&quot; value=&quot;{{ $student-&gt;studentdetail-&gt;alter_contact }}&quot;&gt;&lt;br&gt;&lt;br&gt;
        Address &lt;input type=&quot;text&quot; name=&quot;address&quot; value=&quot;{{ $student-&gt;studentdetail-&gt;address }}&quot;&gt;&lt;br&gt;&lt;br&gt;
        Course &lt;input type=&quot;text&quot; name=&quot;course&quot; value=&quot;{{ $student-&gt;studentdetail-&gt;course }}&quot;&gt;&lt;br&gt;&lt;br&gt;
        &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
    &lt;/form&gt;

index.balde.php

&lt;form action=&quot;{{ route(&#39;one_one.edit&#39;,$student-&gt;id) }}&quot;&gt;&lt;button type=&quot;submit&quot;&gt;Edit&lt;/button&gt;&lt;/form&gt;
                &lt;form action=&quot;{{ route(&#39;one_one.destroy&#39;,$student-&gt;id) }}&quot; method=&quot;POST&quot;&gt;
                    @csrf
                    @method(&#39;DELETE&#39;)
                    &lt;button type=&quot;submit&quot;&gt;Delete&lt;/button&gt;&lt;/form&gt;

Controller

public function edit(Student $student)
    {
        return view(&#39;one_one.edit&#39;,compact(&#39;student&#39;));
    }
    public function update(Request $request, Student $student)
    {
        $student=Student::update([
            &#39;name&#39;=&gt;$request-&gt;name,
            &#39;contact&#39;=&gt;$request-&gt;contact,
            &#39;email&#39;=&gt;$request-&gt;email,
        ]);

        $student-&gt;studentdetail()-&gt;update([
            &#39;alter_contact&#39;=&gt;$request-&gt;alter_contact,
            &#39;address&#39;=&gt;$request-&gt;address,
            &#39;course&#39;=&gt;$request-&gt;course,
        ]);
        return redirect()-&gt;route(&#39;one_one.index&#39;)-&gt;with(&#39;success&#39;,&#39;Data updated successfully&#39;);
    }

I also tried to pass $student->id as array but it is also not working.

答案1

得分: 0

你正在将一个空的不存在的模型传递给你的视图。对于你的 edit 方法,没有发生隐式路由模型绑定。路由参数的名称与控制器的 edit 方法的类型提示参数不匹配。你得到的是方法注入(一个新实例的 Student 模型),而不是隐式路由模型绑定。

你资源路由的路由参数是 one_one(因为这是资源的名称),而不是 student。你的方法签名必须调整以进行隐式路由模型绑定:

public function edit(Student $one_one)

Laravel 10.x 文档 - 路由 - 路由模型绑定 - 隐式绑定

英文:

You are passing an empty non-existing model to your view. There is no Implicit Route Model Binding happening for your edit method. The route parameter name is not matching the typehinted parameter of the Controller's edit method. You are getting method injection (a new instance of a Student model) instead of Implicit Route Model Binding

Your route parameter for your resource routes is one_one(as that is the name of the resource) not student. Your method signature would have to be adjusted for Implicit Route Model Binding:

public function edit(Student $one_one)

Laravel 10.x Docs - Routing - Route Model Binding - Implicit Binding

huangapple
  • 本文由 发表于 2023年7月3日 21:28:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定