【发布时间】:2019-05-16 07:15:34
【问题描述】:
我需要用 laravel 5.7 做一个 api,这个 api 本身不是问题。
我需要对当前模型和嵌套关系对象进行严格验证以保存,例如。
想象一个具有关系 cmets 的模型帖子。 我已将我的数据存储在数据库中:
[
{id: 1, user_id: 1, title: 'my title post', comments: [{id: 5, comment: 'my comment for post id 1'}]},
{id: 2, user_id: 1, title: 'my second title post', comments: [{id: 15, comment: 'my comment for post id 2'}]},
{id: 8, user_id: 2, title: 'my third title post', comments: [{id: 25, comment: 'my comment for post id 8'}]}
]
我登录api的user_id是1
如果我尝试发送POST http 来更新帖子和评论,我如何验证登录api 的用户是每个对象的所有者?帖子示例:
POST 更新。
[
{id: 1, user_id: 1, title: 'my title post modified', comments: [{id: 5, comment: 'my comment for post id 1'}]},
{id: 1, user_id: 1, title: 'my title post modified 2', comments: [{id: 25, comment: 'my comment for post id 1'}]},
{`id: 8`, user_id: 2, title: 'my title post', comments: [{`id: 25`, comment: 'my comment for post id 1'}]},
]
示例如何显示,我只能修改数组的第一个和第二个对象,但不能修改第二个对象中的注释。
希望我正确地表达了自己。
【问题讨论】:
-
您在 Posts 表中没有一列来保存写入吗?喜欢 author_id?
-
@FatemehMajd 它的
user_id在帖子表中
标签: laravel api validation