如果请求值为空,则Laravel设置默认值

如果请求值为空,则Laravel设置默认值,laravel,request,migration,default-value,Laravel,Request,Migration,Default Value,我正在尝试将默认值设置为laravel迁移。我正在使用SQLite 当我尝试插入一个带有指定默认值的字段的记录时,返回一个错误:SQL错误或缺少数据库(表customers没有名为country的列) 这是migrationscript: php Schema::create('customers', function (Blueprint $table) { $table->id(); $table->string('name');

我正在尝试将默认值设置为laravel迁移。我正在使用SQLite

当我尝试插入一个带有指定默认值的字段的记录时,返回一个错误:SQL错误或缺少数据库(表customers没有名为country的列)

这是migrationscript:

php
Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('contact_person_first_name');
            $table->string('contact_person_name');
            $table->string('contact_person_email_address');
            $table->string('address');
            $table->string('zipCode');
            $table->string('city');
            $table->string('country')->default("BELGIË");
            $table->string('vat_number')->default("BE");
            $table->timestamps();
控制员:

*将新创建的资源存储在存储器中。
*
*@param CreateCustomerRequest$请求
*@return\light\Http\RedirectResponse
*/
公共函数存储(CreateCustomerRequest$请求)
{
客户::创建($request->validated());
return response()->重定向到('/customers');
}
请求:

/**
*获取应用于请求的验证规则。
*
*@return数组
*/
公共职能规则()
{
返回[
'name'=>'必需|字符串|最小值:2 |最大值:255',
“联系人姓名”=>“必填|字符串|最小值:2 |最大值:255”,
“联系人姓名”=>“必填”|字符串|最小值:2 |最大值:255”,
“联系人\电子邮件\地址”=>“必需的\电子邮件”,
'地址'=>'必填|字符串|最小值:2 |最大值:255',
'zipCode'=>'必需|字符串|最小值:4 |最大值:10',
“城市”=>“必需|字符串|最小值:2 |最大值:255”,
“国家”=>“可空|字符串|最小值:2 |最大值:255”,
“增值税编号”=>“可为空|最小值:12 |最大值:12”
];
}
我要执行的sql脚本:

来自数据库GUI的一些屏幕截图


您已经在数据库层中设置了一个默认值。但它不能为空。这就是问题的根源。您的请求国家/地区为空,并且在创建新行时分配该空值。您必须使用nullable来插入null值,或者在创建时必须设置一个值

用于数据库

$table->string('country')->nullable()->default("BELGIË");
对于你的情况来说,这不是一个好的解决方案。所以你可以用一些东西

Customer::create([
    'name' => $request->name,
    'country' => $request->country ?? 'BELGIË',
    //other attributes
]);

我通过在迁移中添加nullable解决了这个问题,然后检查value是否为null

$customer=customer::create($request->validated());
如果($customer->country==null)$customer->country=“BELGIË”;
如果($customer->vat\u number==null)$customer->vat\u number=“BE”;
$customer->save();
这比将每个值都放入create中要省力。

#案例1:
如果输入为null=>则存储默认值

#解决方案1-在保存前直接设置默认值

public function store(CreateCustomerRequest $request)
{
    // Retrieve the validated input data...
    $validated = $request->validated();
    
    $validated['need_to_have_default_value'] = $validated['need_to_have_default_value'] ?? $defaultValue; // Replace $defaultValue with value that you want to set as default
    Customer::create(validated );
#解决方案2-在FormRequest=>prepareForValidation()中设置默认值

如果您不熟悉laravel表单请求验证,请参见以下链接:


#案例2:
如果输入为null=>则存储null

对于这种情况,您只需要将
nullable()
添加到迁移中

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

#情况3:
如果未设置输入/发送=>存储默认值

在这种情况下,
default()
将有意义;因此,只需在迁移过程中将
default()
添加到您的字段中即可。如下图所示:

$table->string('set_default_if_not_present')->default('any_default_value');

#情况4:
如果未设置输入/send=>存储空值

此案例与案例2重复


希望有帮助

这回答了你的问题吗?这只是将值设置为null。。它没有设置任何默认值,但您正在做相同的事情两次。创建,然后再次更新。你可以一次完成。有时候多写几行代码更好:)你是对的,但是如果模型有20个字段,那么维护起来就比较困难,而且对于模型的未来更新来说,这总是有效的。否则我也必须更新这个函数。
$table->string('set_default_if_not_present')->default('any_default_value');