【发布时间】:2021-01-15 22:43:53
【问题描述】:
我正在尝试为 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'
];
}
【问题讨论】:
-
这能回答你的问题吗? Set default to NULL with laravel migration
-
这只是将值设置为null..它没有设置任何默认值
标签: laravel request migration default-value