Laravel在传递变量时未定义变量

时间:2016-07-04 14:57:42

标签: php laravel

初学者问题:

我将$ WordsRow变量从WordsController传递给results2刀片。 $ wordsRow在单词表中包含一行。

WordsController代码:

$wordsRow = Words::where(DB::raw('body'),'LIKE', "%{$body}%")->get();

        return view('results2', [
    'message' => $message ,
    'wordsRow' => $wordsRow]);

然后在results2刀片中,将wordsRow的body和id列传递给dashboard2刀片。

@if (isset($wordsRow))
     @foreach ($wordsRow as $wordsRow)
<a href="{{route('dashboard2',[
     'wordsRowB'=>$wordsRow->body,
    'wordsRowId'=>$wordsRow->id])}}">{{$wordsRow->body}}</a> <br>
     @endforeach
@endif

然后在dashboard2刀片中,我遇到如下问题:

如果我使用带有空操作<form action="#" method="post">的表单,则不会出现任何问题,并且仪表板视图会打开而没有任何问题。 如果我使用:

<form action="{{route('post.create',['wordID' => $wordsRowId])}}" method="post">

我收到以下错误:

aadedc1cbff958325ddae8e9ce9778562c4daf4a.php第83行中的ErrorException: 未定义的变量:wordsRowId(查看:D:\ wamp \ www \ Xxxxx \ resources \ views \ dashboard2.blade.php)

任何帮助?

1 个答案:

答案 0 :(得分:0)

问题在于Gerard reches提到的路线。

我修改了dashboard2路由以保存2个变量,并修改了控制器以接收2个变量,然后将它们返回到Dashboard2视图,如下所示:

控制器代码:

 public function getDashboard($wordsRowId, $wordsRowB)
    {
       $posts = Post::orderBy('created_at', 'desc')->get();
        //$posts=post::all();
        //return view('dashboard');
        return view('dashboard2', [
            'posts' => $posts,
            'wordsRowB'=> $wordsRowB ,
            'wordsRowId'=> $wordsRowId
            ]);

    }

路线代码:

Route::get('/dashboard2/{wordsRowB}/{wordsRowId}', [
    'uses' => 'DashController@getDashboard',
    'as' => 'dashboard2',
    'middleware' => 'auth'
]);