laravel将数据从View传递给控制器​​:缺少[Route:]所需的参数

时间:2018-03-01 07:10:07

标签: php laravel laravel-5 eloquent

我正在尝试将一些数据从我的视图传递到我的控制器,以便使用所选月份显示数据,我不确定我所做的月份数字或Eloquent查询是否正确, 这是我到目前为止所得到的。

我的路线:

Route::get('dcmlog/monthly','LogController@monthly');

Route::resource('dcmlog', 'LogController');

控制器:

public function monthly($id)
{
     $dcmlogs = log::with('users')
     ->whereMonth('created_at', '=', $id))
     ->paginate(15);
    return view('dcmlog.index', compact('dcmlogs'));
}

我的索引视图:

<h>Display a logs by month <h>
<a href="{{action('LogController@monthly'),$post['id'] }}">
{{ $id=Form::selectMonth('month')}}</a>

运行页面时出现以下错误

  

缺少[路线:] [URI:dcmlog / monthly / {id}]所需的参数。

4 个答案:

答案 0 :(得分:2)

将ID参数添加到您的路线

Route::get('dcmlog/monthly/{id}','LogController@monthly');

在此之后,您可以访问控制器中的id值。

您可以在此处查看文档https://laravel.com/docs/5.6/routing#required-parameters

答案 1 :(得分:2)

添加路由器:Route::get('dcmlog/monthly/{id}','LogController@monthly')->name('blah');

在视图id中将<a href="{{action('LogController@monthly'),$post['id'] }}"> var作为参数传递 或{{route('blash',['id'=>$post['id']])}}

答案 2 :(得分:1)

您缺少在路线中添加id。只需添加它就可以修复!

有关详细信息,请参阅此docs

添加它像:

Route::get('dcmlog/monthly/{id}','LogController@monthly'); 

答案 3 :(得分:1)

路线中缺少 id 参数:
Route::get('dcmlog/monthly','LogController@monthly');应该是 Route::get('dcmlog/monthly/{id}','LogController@monthly');

Routing with parameters

和Action语法有错误:
<a href="{{action('LogController@monthly'),$post['id'] }}">应该是 <a href="{{action('LogController@monthly', $post['id']) }}">

URL generation for controllers action