Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 Internal error: Failed to retrieve the default value

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

enter image description here

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!

like image 903
Isuru Avatar asked Mar 15 '17 17:03

Isuru


2 Answers

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.

like image 166
Moshe Katz Avatar answered Nov 04 '22 23:11

Moshe Katz


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'));
}
like image 3
Eric Tucker Avatar answered Nov 04 '22 21:11

Eric Tucker