Laravel 5.4 Internal error: Failed to get default value

I have a form that submits data to a database. And he sends the data to the database. But Laravel gives 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 am using. Check out 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 to use both methods in two different cases. But Laravel every time shows the same error.

 return redirect()->route('contactus.show', $inbox->id); return redirect()->action( ' ContactUsController@show ', ['id' => 11] ); 

And here is the web.php route web.php .

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

I don’t know what the problem is. Any suggestion would be helpful.

Thanks!

+7
php laravel laravel-5
source share
3 answers

You pass the id value to the router when forwarding, but you do not tell the router to use this value in the route. Therefore, the router does not know to pass the id value to the show method.

Change the route this way:

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

Then your redirection should work, and you should redirect it (using the identifier from your example) /contactus/11 .

+7
source share

You do not have the $id defined on the route. You can add this to the route with:

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

OR

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

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

I had the same error, and that was because there was a parameter $request instead of Request $request

0
source share

All Articles