Laravel创建新记录错误

时间:2017-08-31 12:41:46

标签: laravel laravel-5.4

我在创建具有不同验证规则的不同记录时有一些表格。

我的路线(web.php)文件:

Route::group(['middleware' => ['auth']], function () {

  Route::get('/home', 'HomeController@index')->name('home');

  Route::get('/home/info/create/{id}', 'InfoController@create');
  Route::post('/home', 'InfoController@store')->name('home');
  Route::get('/home/info/delete/{id}', 'InfoController@delete');

  Route::get('/home/odbor/create/{id}', 'OdborController@create');
  Route::post('/home', 'OdborController@store')->name('home');
  Route::get('/home/odbor/delete/{id}', 'OdborController@delete');


  Route::get('/home/zamestnanec/create/{id}', 'ZamestnanecController@create');
  Route::post('/home', 'ZamestnanecController@store')->name('home');
  Route::get('/home/zamestnanec/delete/{id}', 'ZamestnanecController@delete');

  Route::get('/home/program/create/{id}', 'ProgramController@create');
  Route::post('/home', 'ProgramController@store')->name('home');
  Route::get('/home/program/delete/{id}', 'ProgramController@delete');
});

创建,存储,删除功能: (功能在所有控制器中都是相似的)

public function create(Fakulta $id)
{
    return view('create.info', compact('id'));
}

public function store(CreateInfoRequest $request)
{

    Info::create($request->all());

    return redirect('home');
}

public function delete($id)
{
    Info::where('id',$id)->delete();
    return redirect('home');
}

问题是,当我想创建新的例如触摸子项按钮后的信息(/ home / info / create)我收到验证错误。验证是使用中间件组(ProgramController @ store)的最后一个Controller中指定的文件,我不知道为什么。但是当我移动Route :: post('/ home','InfoController @ store') - > name('home');在组的最后一行或创建新程序(/ home / program / create)一切正常。

更容易:

触摸提交按钮后不使用此文件

CreateInfoRequest.php文件:

public function rules()
{
    return [
        'title' => 'required',
        'description' => 'required',
        'event_date' => 'required|date|after:today'
    ];

}

但这一个: CreateProgramRequest.php

   public function rules()
{
    return [
        'title' => 'required',
        'titul' => 'required',
        'length' => 'required',
        'forma' => 'required',
        'typ'=> 'required',
        'description' => 'required',
        'fakulta_id'  => 'required',

    ];

}

1 个答案:

答案 0 :(得分:1)

您应该尝试以下更改:

将路线放置为:

Route::post('/home/info/store', 'InfoController@store')->name('infoStore');

而不是

Route::post('/home', 'InfoController@store')->name('home');

因为您为所有人创建相同的邮政路线所以只需更改路线并尝试它可能会解决您的问题。