Laravel 5.4 Internal error: Failed to retrieve the

2019-06-16 07:05发布

问题:

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.

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

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

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

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

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

And here is the route file 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!

回答1:

You are passing the id value 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 id value into the show method.

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.



回答2:

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

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

OR

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

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


回答3:

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