【问题标题】:Laravel - Inserting Two Select Values to DatabaseLaravel - 将两个选择值插入数据库
【发布时间】:2019-05-04 18:58:57
【问题描述】:

我想将目标输入值插入数据库,但它给了我一个错误,请参见此处。问题是什么?

SQLSTATE[23000]:完整性约束违规:1048 列“目标”不能为空(SQL:插入aircraft_flightsflight_numberiata_flight_numberflight_datedeparture_timearrival_time,@ 987654327@、destinationaircraft_id) 值(FK2457、SQ1、2018-03-02T04:03、2018-04-04T04:25、2018-05-06T16:02、菲律宾、4))

这是我的目的地表单输入

 <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>

我的迁移

 $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');

我的控制器

 $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');

我的模特

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

【问题讨论】:

  • 我猜你已经在数据库中创建了destinationNULL。因为destination 列中不允许NULL
  • @PrashantDeshmukh .....它不能为空,因为我已经在目的地插入了一个值
  • $aircraftFlight = new AircraftFlights; 行之前检查您在控制器中的dd($request-&gt;all()) 中得到了什么。
  • 没关系解决它,因为存储功能目标有空间

标签: html mysql laravel laravel-5 eloquent


【解决方案1】:

您在我阅读的内容中缺少destination 输入。 from_location 在您的示例中为 Philippinesdestination 为 NULL,则飞机 ID 为 4

当您将此添加到您的 Model 时,它应该可以解决问题(然后可以传递 NULL)

protected static $strictMode = false;

【讨论】:

  • 那么您的输入有问题(如果您输入数据并且它仍然给出 null)
  • 请尽量不要选择目的地并检查 strictMode false 是否有效
【解决方案2】:

您需要将表单的默认值从 NULL 更改为空字符串

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

或者

您需要更改$table-&gt;string('destination')-&gt;nullable();(允许空值)并再次运行迁移。

$aircraftFlight-&gt;from_location 和您发送空值的位置也会出现同样的问题

【讨论】:

  • 但我不希望它为空我也为每个选择值我不希望该值为空
  • 仍然给我错误。它不能为空,因为我在目标上插入了一个值
【解决方案3】:

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

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

$aircraftFlight-&gt;destination = $request-&gt;input('destination'); 删除空格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    相关资源
    最近更新 更多