web-dev-qa-db-ja.com

Laravel 5.4内部エラー:デフォルト値の取得に失敗しました

データベースにデータを送信するフォームがあります。そして、データベースにデータを送信します。ただし、Laravelは、同じコントローラー内の別のメソッドにリダイレクトしようとするとエラーが発生します。

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

enter image description here

これが私が使っているコントローラーです。 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)
    {
        //
    }
}
_

そして、私は2つの異なる機会に次の両方の方法を試しました。しかし、Laravelは毎回同じエラーを示しています。

_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');
_

何がこの問題を引き起こしているのかわかりません。どんな提案も役に立ちます。

ありがとう!

9
Isuru

リダイレクトでid値をルーターに渡しますが、ルートでこの値を使用するようにルーターに指示していません。したがって、ルーターはid値をshowメソッドに渡すことを知りません。

ルートを次のように変更します。

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

次に、リダイレクトが機能し、(例のIDを使用して)/contactus/11にリダイレクトされるはずです。

8
Moshe Katz

ルートに$idが定義されていません。次の方法でルートに追加できます。

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

[〜#〜]または[〜#〜]

これをgetパラメーターとして渡し、リクエストで取得できます。

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

同じエラーが発生しました。これは、$requestの代わりにパラメータRequest $requestもあったためです。

0
Margus Pala