【问题标题】:How to save INT fields as null in laravel如何在laravel中将INT字段保存为null
【发布时间】:2017-03-13 14:39:48
【问题描述】:

我在保存一些空文本、textarea 字段时遇到错误。 Laravel 形成这个 sql 查询:

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'inside_area' at row 1 (SQL: insert into `ads` (`street`, `quarter`, `building_number`, `inside_area`, `price`, `admin_comment`, `valid_to`, `price_heating`, `rooms`, `floor_number`, `floors_number`, `kitchen_area`, `years`, `public_comment`, `video_url`, `3d_url`, `user_id`, `updated_at`, `created_at`) values (, , , , , , , , , , , , , , , , 1, 2017-03-13 14:33:50, 2017-03-13 14:33:50))

附: db 表不是以 laravel 方式创建的 - 我正在使用现有表,这可能很重要。

更新:问题只存在于 INT 字段,如果它们在保存时有空的表单字段!

表:

CREATE TABLE `ads` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `ad_type_id` int(11) DEFAULT NULL,
  `city_id` int(11) DEFAULT NULL,
  `street` varchar(255) DEFAULT NULL,
  `quarter` varchar(255) DEFAULT NULL,
  `building_number` varchar(255) DEFAULT NULL,
  `inside_area` int(11) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  `show_price_per_meter` int(1) DEFAULT NULL,
  `price_heating` int(10) DEFAULT NULL,
  `admin_comment` text,
  `valid_to` datetime DEFAULT NULL,
  `rooms` int(11) DEFAULT NULL,
  `floor_number` int(11) DEFAULT NULL,
  `floors_number` int(11) DEFAULT NULL,
  `kitchen_area` int(11) DEFAULT NULL,
  `balcony` int(1) DEFAULT NULL,
  `balcony_glazed` int(1) DEFAULT NULL,
  `years` int(11) DEFAULT NULL,
  `dyn_house_type_id` int(11) DEFAULT NULL,
  `dyn_heating_id` int(11) DEFAULT NULL,
  `dyn_installation_ids` int(11) DEFAULT NULL,
  `public_comment` text,
  `video_url` varchar(11) DEFAULT NULL,
  `3d_url` varchar(11) DEFAULT NULL,
  `available_for_trade` int(1) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

表格:

{!! Form::open(['url'=>'ads']) !!}
    {!! Form::number('inside_area', null, ['class'=>'form-control']) !!}
{!! Form::close() !!}

路线:

Route::resource('ads', 'AdsController');

保存操作:

public function store() {
        $input = Request::all();
        \App\Ad::create($input);
        return redirect('/ads/my_index');
    }

P.S.2 如果我为 inside_area 字段提供任何值,它会正常,下一个错误是 price 字段。

【问题讨论】:

  • 你能添加生成 Laravel 查询的代码吗?
  • 也许您需要将 {inside_area} 值插入为整数? (例如 intval($inside_area))。您放置的 {inside_area} 的值的数据类型可能有些问题。
  • 我已经添加了更多这种情况的代码。如果我为inside_area 字段提供数值,则一切正常,但错误出现在第二个具有空值的 INT 字段。

标签: laravel


【解决方案1】:

您可以对每个整数属性使用属性修改器。在这些 mutators 中,您可以在插入 DB 之前准备数据。

在您的情况下,您应该在模型广告中为每个类型为 INT 的字段创建突变器:

// define a mutator for the field "inside_area"
public function setInsideAreaAttribute($value)
{
    $this->attributes['inside_area'] = (int) $value;
}

我知道为每个 INT 字段创建这样的 mutator 很无聊,但它还可以让您在插入 DB 之前控制用户输入。

您可以在此处找到有关 mutators 的更多信息(目前为最新版本的 Laravel):

【讨论】:

    【解决方案2】:

    也许,您正在将字符串数据传递给“inside_area”变量。因为你的表中有更多的默认空字段,比如'city_id'、'ad_type_id'

    【讨论】:

    • 我忘了说,有些表字段没有表单字段。所以inside_area 是第一个具有 Form 字段的 INT 字段,保存时为空。我已经为 city_id 创建了字段 - 所以如果它是空的,我也会收到错误。
    【解决方案3】:

    另一种方法是使用Iatstuti并在Ad.php模型中提供:

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    use Iatstuti\Database\Support\NullableFields;
    
    class Ad extends Model {
    
        use NullableFields;
    
        public $timestamps = true;
    
        protected $nullable = [
            'inside_area'
        ];
    
        protected $fillable = [
            'user_id',
            'ad_type_id',
            'inside_area'
    
    
        ];
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 2018-01-05
      • 2016-08-10
      • 2018-10-14
      相关资源
      最近更新 更多