我的 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 表單
uj5u.com熱心網友回復:
使資料庫中的欄位可以為空。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();
}
資料庫結果:
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)
uj5u.com熱心網友回復:
嘗試這個:
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430642.html
