douquan9826 2017-01-18 16:09
浏览 92
已采纳

常规错误:1366不正确的整数值:laravel中的单选按钮

I am facing this error while trying to submit my form : General error: 1366 Incorrect integer value : 'important' for column 'importance' at row 1 (SQL: insert into projets (title, dateDebut, dateFin, cout, partenaires_financiers, importance, updated_at, created_at) values (MyTest, 1996/08/15, 1998/06/28, 15900, paldk ksjd, important, 2017-01-18 15:31:57, 2017-01-18 15:31:57))

Here's the code I am using :

submit.blade.php :

<label for="Import">Importance</label>
<div class="form-group"> 

 <label class="radio-inline">
 <input type="radio" id="tres_important" name="importance" value="tres_important">Très important</label>

<label class="radio-inline">
<input type="radio" id="important" name="importance" value="important">Important</label>
</div>

the migration "create_projects_table.php" :

class CreateProjetsTable extends Migration
{

public function up()
{
Schema::create('projets', function (Blueprint $table) {
    $table->increments('id',true);
    $table->string('title');
    $table->date('dateDebut');
    $table->date('dateFin');
    $table->float('cout');          
    $table->string('partenaires_financiers');
    $table->integer('importance'); 
    $table->timestamps();
 });
 }   

"route.php" :

Auth::routes();
Route::get('/home', 'HomeController@index');
Route::get('/', function () {
$projets = \App\Projet::all();
return view('welcome', compact('projets'));
});

Route::get('/submit', function () {
   return view('submit');
});
Route::post('submit/projects', 'Project_Controller@store');

"Project_Controller.php" :

public function store(Request $request)
{

$validator = Validator::make($request->all(), [
    'title' => 'required|max:255',
    'dateDebut' => 'required|max:255',
    'dateFin' => 'required|max:255',
    'cout' => 'required|max:255',
    'importance' => 'required',
    //'etude' => 'required',
    'partenaires_financiers' => 'required',

]);

if ($validator->fails()) {
 return back()
    ->withInput()
    ->withErrors($validator);
}     

$projet = new Projet;

$projet->title = $request->title;
$projet->dateDebut = $request->dateDebut;
$projet->dateFin = $request->dateFin;
$projet->cout = $request->cout;
$projet->partenaires_financiers = $request->partenaires_financiers;
$projet->importance = $request->importance;

$projet->save();

return redirect('/submit');
}
}

I was using this code in my controller before adding the "store" function and it was working :

$create_projets = CreateProjets::find($id);
$data['importance'] = $create_projets->importance;
return view('submit', $data);

I know that the error is caused by the "radio buttons"

Does anyone have an idea how to fixe that please ?

  • 写回答

1条回答 默认 最新

  • doulongdan2264 2017-01-18 16:19
    关注

    in your create_projects_table you want to change

    $table->integer('importance'); to $table->string('importance');

    or if imporantance is binary value like 0/1

    $projet->importance = $request->importance === 'important' ? 1 : 0; Would assign importance to be 1 if the radio selected as important. (not sure if tres important is more or less important)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作