Laravel - Inserting Two Select Values to Database

时间:2018-12-03 13:07:01

标签: html mysql laravel laravel-5 eloquent

I want to insert the destination input value to database but it is giving me an error see here. what is the problem?

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'destination' cannot be null (SQL: insert into aircraft_flights (flight_number, iata_flight_number, flight_date, departure_time, arrival_time, from_location, destination, aircraft_id) values (FK2457, SQ1, 2018-03-02T04:03, 2018-04-04T04:25, 2018-05-06T16:02, Philippines, , 4))

here is my Form Input of Destination

 <div class="col-md-4 col-sm-4 ">
    {{Form::label('from_location', 'From')}} 
    {{Form::select('from_location', trans('countries'),null,['class' => 'form-control', 'placeholder' => 'Select where you From'])}}<br>
</div>
<br>

<div class="col-md-4 col-sm-4 ">
    {{Form::label('destination', 'Destination Country')}} 
    {{Form::select('destination', trans('countries'),null,['class' => 'form-control', 'placeholder' => 'Select where is your Destination'])}}<br>
</div>
<br>

my Migration

 $table->increments('id');
        $table->string('flight_number');
        $table->string('iata_flight_number');
        $table->dateTime('flight_date');
        $table->dateTime('departure_time');
        $table->dateTime('arrival_time');
        $table->string('from_location');
        $table->string('destination');
        $table->integer('aircraft_id');

my Controller

 $aircraftFlight = new AircraftFlights;
    $aircraftFlight->flight_number = $request->input('flight_number');
    $aircraftFlight->iata_flight_number = $request->input('iata_flight_number');
    $aircraftFlight->flight_date     = $request->input('flight_date');
    $aircraftFlight->departure_time = $request->input('departure_time');
    $aircraftFlight->arrival_time = $request->input('arrival_time');
    $aircraftFlight->from_location = $request->input('from_location');
    $aircraftFlight->destination     = $request->input('destination ');
    $aircraftFlight->aircraft_id = $request->input('aircraft_id');

    $aircraftFlight->save();

    return redirect('/admin/aircraftflights')->with('success', 'Flight Created');

my Model

protected $table = 'aircraft_flights';
// Primary Key
public $primaryKey = 'id';
// Timestamps
public $timestamps = false;

3 个答案:

答案 0 :(得分:0)

you are missing destination input from what I read. from_location is Philippines in your example and destination is NULL then aircraft_id is 4.

When you add this to your Model it should fix the problem (NULL can be passed then)

protected static $strictMode = false;

答案 1 :(得分:0)

You need to change default value for the form from NULL to empty string

{{Form::select('destination', trans('countries'),'',['class' => 'form-control', 'placeholder' => 'Select where is your Destination'])}}<br>

Or

you need to change $table->string('destination')->nullable(); (allow null values) and run Migration again.

The same issue will be for $aircraftFlight->from_location and where you send null values

答案 2 :(得分:0)

数据库中的hi使目标可为空

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

$aircraftFlight->destination = $request->input('destination'); 删除espace