Laravel 5.4内部错误:无法检索默认值 [英] Laravel 5.4 Internal error: Failed to retrieve the default value

查看:127
本文介绍了Laravel 5.4内部错误:无法检索默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将数据提交到数据库的表格.并将数据提交到数据库.但是Laravel尝试将其重定向到同一控制器中的另一个方法时会产生错误.

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

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

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

我尝试了以下两种方法,分别在两种不同的情况下使用.但是Laravel每次都会显示相同的错误.

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]
);

这是路由文件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.

谢谢!

推荐答案

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

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.

更改路线,如下所示:

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

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

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

这篇关于Laravel 5.4内部错误:无法检索默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆