如何解决如果请求值为空,Laravel设置默认值
我正在尝试为laravel迁移设置默认值。我正在使用sqlite。
当我尝试使用分配了默认值的字段插入记录时,将返回错误:sql错误或缺少数据库(表客户没有名为country的列)
这是迁移脚本:
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();
控制器:
* Store a newly created resource in storage.
*
* @param CreateCustomerRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(CreateCustomerRequest $request)
{
Customer::create($request->validated());
return response()->redirectTo('/customers');
}
请求:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:2|max:255','contact_person_first_name' => 'required|string|min:2|max:255','contact_person_name' => 'required|string|min:2|max:255','contact_person_email_address' => 'required|email','address' => 'required|string|min:2|max:255','zipCode' => 'required|string|min:4|max:10','city' => 'required|string|min:2|max:255','country' => 'nullable|string|min:2|max:255','vat_number' => 'nullable|min:12|max:12'
];
}
我要执行的sql脚本:
数据库GUI中的一些屏幕截图
解决方法
您已在数据库层中设置了默认值。但它不能为空。这导致了这里的问题。您的请求国家/地区为空,并且在创建新行时要分配该空值。您要么必须使用nullable来插入null值,要么必须在创建时设置一个值。
对于数据库
$table->string('country')->nullable()->default("BELGIË");
对于您的情况,这不是一个好的解决方案。所以你可以使用一些东西
Customer::create([
'name' => $request->name,'country' => $request->country ?? 'BELGIË',//other attributes
]);
,
我通过在迁移中添加nullable来解决它,然后检查value是否为空。
$customer = Customer::create($request->validated());
if ($customer->country == null) $customer->country= "BELGIË";
if ($customer->vat_number == null) $customer->vat_number= "BE";
$customer->save();
与将每个值都放入创建中相比,这省事得多。
,#Case 1:if input is null => store a default value
#Solution 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 );
#Solution 2 - 在 FormRequest => prepareForValidation() 中设置默认值
如果您不熟悉 laravel form request validation
,请参阅此链接:https://laravel.com/docs/8.x/validation#form-request-validation
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
$this->merge([
'need_to_have_default_value' => $this->need_to_have_default_value ?? $defaultValue // Replace $defaultValue with value that you want to set as default
]);
}
#Case 2:if input is null => store null
对于这种情况,您只需要将 nullable()
添加到您的迁移中。
$table->string('set_null_if_null')->nullable();
#Case 3:if input is not set/send => store default value
在这种情况下,default()
是有意义的;所以它只需要在迁移中将 default()
添加到您的字段中。如下图:
$table->string('set_default_if_not_present')->default('any_default_value');
#Case 4:if input is not set/send => store null value
此案例与案例 2
重复希望有所帮助!
,另一种方式:在模型类中定义 Mutator。
您可以使用 Mutators 定义将什么设置为给定值的数据库值。
查看 Laravel/Docs/Eloquent/Mutators:https://laravel.com/docs/8.x/eloquent-mutators
在 Customer.php 中添加方法。
public function setCountryAttribute($value)
{
$this->attributes['country'] = $value ?? 'BELGIË';
}
public function setVatNumberAttribute($value)
{
$this->attributes['vat_number'] = $value ?? 'BE';
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。