【问题标题】:Missing required parameters for route缺少路由所需的参数
【发布时间】:2017-02-27 16:50:03
【问题描述】:

我想问如何解决这个错误。我有这个错误 Missing required parameters for [Route: userprofile.edit] [URI: userprofile/{userprofile}/edit]. (View: C:\xampp\htdocs\code\task1\resources\views\userprofile\create.blade.php)

我在查看userprofile\create.blade.php 中有用户信息,提交按钮后我想重新定位userprofile\edit.blade.php 上的页面

这是我的userProfileController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\User;

class userProfileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("userprofile.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::find($id);
        return view('userprofile.edit')->withUser($user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

也是此视图的路径

Route::group(['namespace' => 'User'], function(){
 Route::get('/userprofile/{id}/edit', ['as' => 'userprofile.edit', 'uses' => 'userProfileController@edit']);
});


Route::resource('userprofile','userProfileController');

还有我在create.blade.php按钮中的路由操作

<input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">

如果您知道为什么会出现此错误,请写信给我! 谢谢。

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    您不能向输入字段添加操作。将操作添加到 form 标记。您没有在表单操作中传递任何 id。像我通过 id = 1

    一样将 id 添加到您的操作中
    <form method="post" action="{{ route('userprofile.edit', ['id' => 1]) }}">
    

    更新

    首先,您必须使用给定的数据创建一个用户,然后使用生成的 id 重定向到编辑页面。

    【讨论】:

    • 提交按钮后,我有与更改路由 localhost:8080/code/task1/public/userprofile/create?user_name=admin%40example.com 相同的视图
    【解决方案2】:

    你需要为edit和其他方法传入的id设置一个默认值,像这样:

    public function edit($id = null)
    {
        $user = User::find($id);
        return view('userprofile.edit')->withUser($user);
    }
    

    在这里,我们将id 的默认值设置为null。接下来,检查您的方法是否为 null,如果是,则返回错误或在这种情况下您想做的任何事情:

    public function edit($id = null)
    {
        if(!$id)
          return back()->with(['error'=>'No document selected for editing.']);
    
        $user = User::find($id);
        return view('userprofile.edit')->withUser($user);
    }
    

    if(!$id) return back()->with(['error'=>'No document selected for editing.']);

    这是您的检查,如果 id 为空,我们只需返回并通知用户(您必须检查刀片视图中的错误并在存在时显示)。

    【讨论】:

    • 我在设置 id 的值时没有问题,该方法给了我正确的 id,但是我在将页面重新定位到 edit.blade.php 时遇到了问题
    【解决方案3】:

    而不是

    <input type="submit" class="btn btn-primary" action="{{ route('userprofile.edit') }}" value="Edit profile">
    

    使用

    <a href="{{url('/userprofile/'.PROFILE ID.'/edit')" class="btn btn-primary" >Edit profile</a>
    

    PROFILE ID 必须是 id

    【讨论】:

    猜你喜欢
    • 2021-09-02
    • 2021-09-19
    • 2019-01-09
    • 2020-08-07
    • 2020-12-25
    • 1970-01-01
    • 2022-01-21
    • 2020-11-01
    相关资源
    最近更新 更多