繁体   English   中英

缺少路线所需的参数

[英]Missing required parameters for route

我想问一下如何解决这个错误。 我有此错误[Route: userprofile.edit] [URI: userprofile/{userprofile}/edit]. (View: C:\\xampp\\htdocs\\code\\task1\\resources\\views\\userprofile\\create.blade.php)缺少必需的参数[Route: userprofile.edit] [URI: userprofile/{userprofile}/edit]. (View: C:\\xampp\\htdocs\\code\\task1\\resources\\views\\userprofile\\create.blade.php) [Route: userprofile.edit] [URI: userprofile/{userprofile}/edit]. (View: C:\\xampp\\htdocs\\code\\task1\\resources\\views\\userprofile\\create.blade.php)

我在view 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">

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

您无法将操作添加到输入字段。 form标签添加操作。 您没有在表单操作中传递任何ID 将id添加到您的操作中,就像我已通过id = 1

<form method="post" action="{{ route('userprofile.edit', ['id' => 1]) }}">

更新资料

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

您需要为传递给edit和其他方法的id设置默认值,如下所示:

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

在这里,我们将id的默认值设置为null 接下来,检查您的方法是否为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为null,我们仅返回并通知用户(您必须在刀片视图中检查错误,如果存在,则将其显示)。

代替

<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>

个人资料ID必须为ID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM