简体   繁体   中英

Form request in Laravel

1.How to deal with question mark in form request in laravel ?

2.Should I set the default value each input tag ?

create.blade.php

<form class="form-horizontal row-fluid" name="user_input_form" action="/bonds" method="POST">
  @csrf
  <p><strong>Apply</strong></p>
  <div class="control-group">
      <label class="control-label">investment</label>
      <div class="controls">
          <div class="input-append">
              <input type="text" name="investment" id="investment" placeholder="" class="w-full span12">
          </div>
      </div>
  </div>
  <div class="control-group">
      <label class="control-label">investment_name</label>
      <div class="controls">
          <div class="input-append">
              <input type="text" name="investment_name" id="investment_name" placeholder="" class="span12">
          </div>
      </div>
  </div>
  <div class="control-group">
      <label class="control-label">i_id</label>
      <div class="controls">
          <div class="input-append">
              <input type="text" name="i_id" id="i_id" placeholder="" class="span12">
          </div>
      </div>
  </div>
  <div class="control-group">
      <label class="control-label">price</label>
      <div class="controls">
          <div class="input-append">
              <input type="text" value="" name="price" id="price" placeholder="" class="span12" onchange="getagreement();"><span class="add-on"></span>
          </div>
      </div>
  </div>

BondsController.php

public function store(Request $request)
    {

      $bond = Bond::create([
          'investment' => request('investment'),
          'investment_name' => request('investment_name'),
          'i_id' => request('i_id'),
          'price' => request('price'),
          'p_goal' => request('p_goal'),
          'c_date' => request('c_date'),
          'c_date2' => request('c_date2'),
          'c_date3' => request('c_date3'),
          'agreement' => request('agreement'),
          'goal_in' => request('goal_in'),
...
}

The Error Like This

Illuminate\\Database\\QueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'investment' cannot be null (SQL: insert into bonds ( investment , investment_name , i_id , price , p_goal , c_date , c_date2 , c_date3 , agreement , goal_in , goal_out , c_date4 , invest_date , p_price , t_price , contract , c_date5 , confirm1 , confirm2 , confirm3 , confirm4 , confirm5 , law_office , etc , address , phone , birth , email , updated_at , created_at )
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 27.5, ?, 1, ?, 1, 1, 1, 1, 1, ?, ?, ?, ?, ?, ?, 2020-01-26 04:02:42, 2020-01-26 04:02:42))

The request('investment') is nullable, however your field investment in bonds cannot be nullable. So you can use validator to check the fields.Or set the default value in this field.

Use validator:

try to use validator like this:

public function store(Request $request)
    {
      $this->validate($request, [
          'investment' => 'required',
          ...
      ]);


      $bond = Bond::create([
          ....
}

Set default value:

$bond = Bond::create([
   'investment' => $request->input('invenstment', 'your_default_value');
   ...
]);

Change the field to nullable:

$table->string('investment')->nullable();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM