如果未填充,则将input设置为空字符串或null,并使用laravel将其他数据保存在vue中



我的newinsight中大约有七个输入字段。我希望其中一些输入字段是可选的,无论它是字段还是字段,帖子都应该保存到数据库中,如果我保留一些输入为空,我会得到错误500。我试过了,似乎不起作用,谁也帮不上忙。下面是我的代码。

我的控制器

public function store(Request $request)
{
$insight= new Insight();
$insight->slug = $request->slug;
$insight->body = $request->body;
$insight->head1 = $request->head1;
$insight->paragraph1 = $request->paragraph1;
$insight->head2 = $request->head2;
$insight->paragraph2 = $request->paragraph2;
$insight->head3 = $request->head3;
$insight->paragraph3 = $request->paragraph3;
$insight->head4 = $request->head4;
$insight->paragraph4 = $request->paragraph4;
$insight->head5 = $request->head5;
$insight->paragraph5 = $request->paragraph5;
$insight->head6 = $request->head6;
$insight->paragraph6 = $request->paragraph6;
$insight->head7 = $request->head7;
$insight->paragraph7 = $request->paragraph7;
if($request->hasFile('image')){
$file = $request->file('image');
$file_name = time(). '.' . $file->getClientOriginalName();
$file->move(public_path('img/'),$file_name);
$insight->image = 'img/' . $file_name;

}

$insight->save();
return response()->json(['message'=>'Saved Successfully'],200); 
}

表单

data() {
return {
form: new Form({
image:null,
slug:'',
body: '',
head1:'',
paragraph1: '',
head2:'',
paragraph2: '',
head3:'',
paragraph3: '',
head4:'',
paragraph4: '',
head5:'',
paragraph5: '',
head6:'',
paragraph6: '',
head7:'',
paragraph7: '',
}),
show: false
}
},
methods: {
onChange(e){
const file = e.target.files[0]
this.form.image = file
console.log("selected file", file) 
},
submit(){
this.form.post('/api/insight', {
transfromRequest: [function(data, headers){
return objectToFormData(data)
}],
onUploadProgress: e =>{
console.log(e,)
}
}).then(({data})=>{
console.log(data)
})
console.log(this.form)
this.form = ''
this.form.image = ''
},

以上是我的vue-js表单

使数据库中的字段可以为null。如果不设置字段,则字段将为NULL

迁移:

public function up()
{
Schema::create('insights', function (Blueprint $table) {
$table->id();
$table->string('body')->nullable();
$table->string('head1')->nullable();
$table->string('paragraph1')->nullable();
$table->string('head2')->nullable();
//...etc
$table->timestamps();
});
}

你可以做到没有错误:

public function store() {
$insight = new Insight();
$insight->save();
}

DB结果:

mysql> select * from insights;
+----+------+-------+------------+-------+---------------------+---------------------+
| id | body | head1 | paragraph1 | head2 | created_at          | updated_at          |
+----+------+-------+------------+-------+---------------------+---------------------+
|  1 | NULL | NULL  | NULL       | NULL  | 2022-02-22 07:38:58 | 2022-02-22 07:38:58 |
+----+------+-------+------------+-------+---------------------+---------------------+
1 row in set (0.00 sec)

试试这个:

public function store(Request $request)
{
// validate your compulsory field here.
$insight= new Insight();
foreach($request->all() as $key => $value){
$insight[$key] = $value;
}
unset($insight[_token]); // if that's your csrf name
if($request->hasFile('image')){
$file = $request->file('image');
$file_name = time(). '.' . $file->getClientOriginalName();
$file->move(public_path('img/'),$file_name);
$insight[image] = 'img/' . $file_name;

}

$insight->save();
return response()->json(['message'=>'Saved Successfully'],200); 
}

最新更新