如何在laravel中将INT字段保存为null

时间:2017-03-13 14:39:49

标签: laravel

我在保存一些空文本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))

P.S。 db table不是以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:

{!! 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字段。

3 个答案:

答案 0 :(得分:2)

您可以为每个整数属性使用属性mutator。在这些mutator中,您可以在插入DB之前准备数据。

在您的情况下,您应该在模型广告中为INT:

类型的每个字段创建mutator
// define a mutator for the field "inside_area"
public function setInsideAreaAttribute($value)
{
    $this->attributes['inside_area'] = (int) $value;
}

据我所知,为每个INT字段创建一个这样的mutator很无聊,但它也会授予你在插入数据库之前控制用户输入的能力。

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

另一种方法是使用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'


    ];

}