Laravel 5.4 内部错误:无法检索默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42816437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:34:06  来源:igfitidea点击:

Laravel 5.4 Internal error: Failed to retrieve the default value

phplaravellaravel-5laravel-5.4

提问by Isuru

I have a form which submits data to the database. And it submits data to the database. But Laravel yields an error when it tries to redirect it to another method in the same controller.

我有一个将数据提交到数据库的表单。并将数据提交到数据库。但是当 Laravel 试图将它重定向到同一个控制器中的另一个方法时,它会产生一个错误。

ReflectionException in RouteDependencyResolverTrait.php line 57:
Internal error: Failed to retrieve the default value

enter image description here

在此处输入图片说明

Here is the controller I use. Please check the public function store(Request $request)method.

这是我使用的控制器。请检查public function store(Request $request)方法。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Inbox;

class ContactUsController 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('pages.contactus');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // validate the data
        // store the data in the database
        // redirect to another page
        $this->validate($request, [
            'name' => 'required|max:255',
            'email' => 'required',
            'telephone' => 'required',
            'message' => 'required',
        ]);

        $inbox = new Inbox;

        $inbox->name = $request->name;
        $inbox->email = $request->email;
        $inbox->telephone = $request->telephone;
        $inbox->message = $request->message;
        $inbox->isnew = 1;
        $inbox->category = $request->category;

        $inbox->save();

        // redirects to the route
        //return redirect()->route('contactus.show', $inbox->id);

        return redirect()->action(
            'ContactUsController@show', ['id' => 11]
        );

    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 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)
    {
        //
    }
}

And I tried, both of following methods in two different occasions. But Laravel shows the same error each time.

我在两个不同的场合尝试了以下两种方法。但是 Laravel 每次都显示相同的错误。

return redirect()->route('contactus.show', $inbox->id);

return redirect()->action(
   'ContactUsController@show', ['id' => 11]
);

And here is the route file web.php.

这是路由文件web.php

Route::get('contactus', 'ContactUsController@create');
Route::get('contactus/show', 'ContactUsController@show');
Route::post('contactus/store', 'ContactUsController@store');

I have no idea what cause this problem. Any suggestion will be helpful.

我不知道是什么导致了这个问题。任何建议都会有所帮助。

Thanks!

谢谢!

采纳答案by Moshe Katz

You are passing the idvalue to the router in the redirect, but you are not telling the router to use this value in the route. Therefore, the router does not know to pass the idvalue into the showmethod.

id在重定向中将值传递给路由器,但您没有告诉路由器在路由中使用此值。因此,路由器不知道将id值传递给show方法。

Change the route to look like this:

将路由更改为如下所示:

Route::get('contactus/{id}', 'ContactUsController@show');

Then your redirect should work, and you should be redirected to (using the ID from your example) /contactus/11.

然后您的重定向应该可以工作,并且您应该被重定向到(使用您示例中的 ID)/contactus/11

回答by Eric Tucker

You don't have the $iddefined on the route. You can either add that to the route with:

您没有$id在路线上定义。您可以将其添加到路由中:

Route::get('contactus/show/{id}', 'ContactUsController@show');

OR

或者

You can pass it as a get parameter and retrieve it in the request:

您可以将其作为 get 参数传递并在请求中检索它:

public function show(Request $request)
{
    print_r($request->input('id'));
}

回答by Margus Pala

I had same error and it was because there was also parameter $requestinstead of Request $request

我有同样的错误,这是因为还有参数$request而不是Request $request