我正在嘗試提交一篇簡單的博客文章。我使用請求對象作為 DTO 傳遞數(shù)據(jù)。public function store(CreateBlogRequest $createBlogRequest){ $user = User::find(1); $post = $user->posts()->create([$createBlogRequest]);}我收到以下錯誤:"message": "發(fā)現(xiàn)意外數(shù)據(jù)。\n發(fā)現(xiàn)意外數(shù)據(jù)。\n發(fā)現(xiàn)意外數(shù)據(jù)。\n找不到兩位數(shù)的分鐘\n找不到兩位數(shù)的秒\n尾隨數(shù)據(jù)"但是,當(dāng)我將數(shù)據(jù)作為標(biāo)準(zhǔn)數(shù)組傳遞時,它運行良好。public function store(Request $request){ $user = User::find(1); $post = $user->posts()->create(['title' => $request->title, 'slug' => $request->slug, 'body' => $request->body]);}崗位模型class Post extends Model{ protected $guarded = []; protected $dates = ['created_at','updated_at']; protected $dateFormat = 'Y-m-d H:i:s'; public function user() { return $this->belongsTo('App\User'); }}知道這里有什么問題嗎?
1 回答

慕尼黑8549860
TA貢獻1818條經(jīng)驗 獲得超11個贊
你不能只傳遞請求對象(你也把它包裝在一個數(shù)組中)。該create()
方法需要一個關(guān)聯(lián)數(shù)組。
相反,您可以執(zhí)行在第二個示例中所做的操作?;蛘呦襁@樣:
$post = $user->posts()->create($createBlogRequest->input());
或更明確(更安全):
$post = $user->posts()->create($createBlogRequest->only(['title', 'slug', 'body']));
不過,您可能需要創(chuàng)建字段$fillable
。
- 1 回答
- 0 關(guān)注
- 148 瀏覽
添加回答
舉報
0/150
提交
取消