我正在尝试更新或创建记录。如果用户在数据库中有记录,则只更新记录,如果用户在数据库中没有记录,则只需按输入创建记录。错误是
类型错误:函数App \ Http \ Controllers \ ChoicesController :: store()的参数太少,1传递,正好是2个
public function store(Request $request,$id){
$time = $request->input('time');
$user = Choice::where('user_id','=',Auth::id())->first();
if ($user === null) {
foreach ($request->input('number') as $key => $value) {
Choice::create([
'user_id' => Auth::id(),
'time' => $time,
'topic_id' => $key,
'question_number' => $value,
]);
}
}
else {
$result = Choice::find($id);
foreach ($request->input('number') as $key => $value) {
$result->user_id = Auth::id();
$result->time = $request->$time;
$result->question_number = $request->$value;
$result->topic_id = $request->$key;
}
$result->save();
}
return redirect()->route('choices.index');
}
这些是我的路线。
Route::group(['middleware' => 'auth'], function () {
Route::get('/home', 'HomeController@index')->name('views.home');
Route::post('tests/custom', 'TestsController@custom')->name('tests.custom');
Route::post('exams/custom', 'ExamsController@custom')->name('exams.custom');
Route::post('laws.store',['as'=>'add.category','uses'=>'LawsController@store']);
Route::resource('laws', 'LawsController');
Route::resource('choices', 'ChoicesController');
Route::resource('legals', 'LegalsController');
Route::resource('faqs', 'FAQsController');
Route::resource('guides', 'GuidesController');
Route::resource('materials', 'MaterialsController');
Route::resource('tests', 'TestsController');
Route::resource('exams_results', 'ExamsResultController');
Route::resource('roles', 'RolesController');
Route::resource('videos', 'VideosController');
Route::resource('exams', 'ExamsController');
Route::resource('users', 'UsersController');
Route::resource('user_actions', 'UserActionsController');
Route::resource('topics', 'TopicsController');
Route::resource('questions', 'QuestionsController');
Route::resource('questions_options', 'QuestionsOptionsController');
Route::resource('results', 'ResultsController');
Route::post('users_mass_destroy', ['uses' => 'UsersController@massDestroy', 'as' => 'users.mass_destroy']);
Route::post('roles_mass_destroy', ['uses' => 'RolesController@massDestroy', 'as' => 'roles.mass_destroy']);
Route::post('topics_mass_destroy', ['uses' => 'TopicsController@massDestroy', 'as' => 'topics.mass_destroy']);
Route::post('questions_mass_destroy', ['uses' => 'QuestionsController@massDestroy', 'as' => 'questions.mass_destroy']);
Route::post('questions_options_mass_destroy', ['uses' => 'QuestionsOptionsController@massDestroy', 'as' => 'questions_options.mass_destroy']);
Route::post('results_mass_destroy', ['uses' => 'ResultsController@massDestroy', 'as' => 'results.mass_destroy']);
Route::delete('/delete/law/{id}','LawsController@destroy');
});
答案 0 :(得分:0)
问题似乎是相关的路线,通常存储功能只获得一个参数,这个"请求$ id",但是你的,获得$ request和$ id。因此发生了错误,
标准资源存储功能必须是这样的,
public function store (Request $request)
对于解决方案,
你应该发送" $ id"有请求
或
您应该为此商店功能添加新路线,例如
Route::post('choices/{id}', 'ChoicesController@store')
我认为,解决方案1优于2
我希望这会有所帮助
答案 1 :(得分:-1)
您正在使用资源路由,因此您无法在存储操作中传递$ id参数。
您可以使用laravel本身提供的firstOrCreate()。 https://laravel.com/docs/5.6/eloquent#other-creation-methods