我是laravel
的新手,我已经写了一条路线
Route::resource('contract', 'ContractController');
Route::group(['prefix' => 'contract'], function () {
Route::get('data', 'ContractController@data');
});
我的控制器文件是:
public function data(Datatables $datatables)
{
$contracts = $this->contractRepository->getAll()
->get()
->map(function ($contract) {
return [
'id' => $contract->id,
'start_date' => $contract->start_date,
'end_date' => $contract->end_date,
'description' => $contract->description,
'name' => '',
'user' => '',
];
});
return $datatables->collection($contracts)
->addColumn('actions', '@if(Sentinel::getUser()->hasAccess([\'contracts.write\']) || Sentinel::inRole(\'admin\'))
<a href="{{ url(\'contract/\' . $id . \'/edit\' ) }}" title="{{ trans(\'table.edit\') }}">
<i class="fa fa-fw fa-pencil text-warning"></i> </a>
@endif
@if(Sentinel::getUser()->hasAccess([\'contracts.read\']) || Sentinel::inRole(\'admin\'))
<a href="{{ url(\'contract/\' . $id . \'/show\' ) }}" title="{{ trans(\'table.details\') }}" >
<i class="fa fa-fw fa-eye text-primary"></i> </a>
@endif
@if(Sentinel::getUser()->hasAccess([\'contracts.delete\']) || Sentinel::inRole(\'admin\'))
<a href="{{ url(\'contract/\' . $id . \'/delete\' ) }}" title="{{ trans(\'table.delete\') }}">
<i class="fa fa-fw fa-times text-danger"></i></a>
@endif')
->removeColumn('id')
->escapeColumns( [ 'actions' ] )->make();
}
当我使用网址contract/data
运行时,我收到404 not found
错误。在控制台中,我也遇到错误
No query results for model [App\Models\Contract].
请帮助我解决问题
答案 0 :(得分:1)
只需删除Route::resource('contract', 'ContractController');
或将其放在Route::group(['prefix' => 'contract'], function () {
Route::get('data', 'ContractController@data');
});
之后,就像这样:
Route::group(['prefix' => 'contract'], function () {
Route::get('data', 'ContractController@data');
});
Route::resource('contract', 'ContractController');
您在路由/contract/data
上得到404,因为路由器实际上是从contract/{contract}
的较高路由定向到ContractController@show
中的Route::resource('contract', 'ContractController');
的