Php 用默认值替换空值

Php 用默认值替换空值,php,laravel,validation,eloquent,Php,Laravel,Validation,Eloquent,我一直在努力做一些事情,我本以为这在拉雷维尔是一项非常简单的任务 基本上,我有一个带有多个默认值列的表,允许用户将某些字段留空,而不必到处写“0”。以下是我的迁移查找参考的方式: public function up() { Schema::create('clients', function (Blueprint $table) { $table->id()->autoIncrement(); $table->string('first

我一直在努力做一些事情,我本以为这在拉雷维尔是一项非常简单的任务

基本上,我有一个带有多个默认值列的表,允许用户将某些字段留空,而不必到处写“0”。以下是我的迁移查找参考的方式:

public function up()
{
    Schema::create('clients', function (Blueprint $table) {
        $table->id()->autoIncrement();
        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->date('arrival_date');
        $table->date('departure_date');
        $table->smallInteger('sector')->nullable();
        $table->smallInteger('adults')->default('0');
        $table->smallInteger('children')->default('0');
        $table->smallInteger('electricity')->default('0');
        $table->smallInteger('small_places')->default('0');
        $table->smallInteger('big_places')->default('0');
        $table->smallInteger('discount')->default('0');
        $table->timestamps();
    });
}
protected $attributes = [
    'adults' => 0,
    'children' => 0,
    'electricity' => 0,
    'small_places' => 0,
    'big_places' => 0,
    'discount' => 0
];
问题是-我似乎找不到使用默认值的方法-发送带有空字段的表单会将它们作为“null”值,这些值仍然被视为值,因此尝试创建模型最终会导致关于null值的错误。数据库中的默认值和默认模型值都不起作用,因为它们中的每一个都会被覆盖。供参考的默认模型值:

public function up()
{
    Schema::create('clients', function (Blueprint $table) {
        $table->id()->autoIncrement();
        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->date('arrival_date');
        $table->date('departure_date');
        $table->smallInteger('sector')->nullable();
        $table->smallInteger('adults')->default('0');
        $table->smallInteger('children')->default('0');
        $table->smallInteger('electricity')->default('0');
        $table->smallInteger('small_places')->default('0');
        $table->smallInteger('big_places')->default('0');
        $table->smallInteger('discount')->default('0');
        $table->timestamps();
    });
}
protected $attributes = [
    'adults' => 0,
    'children' => 0,
    'electricity' => 0,
    'small_places' => 0,
    'big_places' => 0,
    'discount' => 0
];
到目前为止,我找到了一些解决方案,没有一个是正确的,我觉得我错过了一些东西

因此,我当前的解决方案如下所示:

public static function add(Request $request)
{
    if (!self::validateRequest($request)) return; // Just checks if required fields are OK, doesn't change values
    Client::create(array_filter($request->all(), function ($item) {
        return !empty($item);
    }));
}
基本上,我只是从$request中删除了所有“null”值,所以使用了数据库中的默认值。这种解决方案的缺点是,当我真的想要分配一个空值时,它会被删除

我发现的第二个解决方案可以很好地工作,但会添加大量代码,并且必须专门针对任何模型。我可以在我的模型中创建一个中间件/只是一个函数,它有一个可为null的键和默认值的列表,如果一个键为null且不可为null,它将用默认值替换它。 这个解决方案的问题是。。。我基本上是在写已经存在于数据库中的规则,但是再次。这将使我的数据库中的可空值和默认值变得多余,因为我已经在代码中完成了所有工作

那么-例如,是否没有办法使用已经编写好的迁移作为验证模板或类似的东西,或者Laravel中是否有任何东西可以解决这个特定问题?如果没有,还有比我更好的方法吗


提前谢谢

有件事我不明白。您希望空字段在默认情况下为0,这样用户就不会在所有字段中都放入0,但您希望它们可以放入空字段。他们怎么能使字段为空?在输入中添加
value=“0”
可能会更容易。

您可以在模型中使用变异体。
公共函数setAdultsAttribute($value){…}
如0中所述被视为空值。尝试输入除0或1以外的其他值。然后看看结果如何。我也尝试过使用1,但它没有改变任何东西。我会尝试使用变异,谢谢!我只希望某些字段为0,就像我为数据库设置的那样。例如,first_name和last_name可以为null,因此如果因为null而删除它们,则会中断工作。value=“0”是执行此操作的一种方法,但如果有人删除此值,则会导致崩溃。