我正在使用 Laravel 5 并实现了一个有效的 Restful API。我试图通过传入这样的数组来允许创建帖子......
post
title
excerpt
content
image
post
title
excerpt
content
image
API 目前非常适合发布一个单独的帖子,但我该如何处理帖子数组?
有没有人有我可以看到的例子?
如果您使用的是资源控制器,您应该有一个PostsController
with 方法store()
。我假设您的请求有效负载是这样的一些 JSON:
{"posts": [{"title":"foo"},{"title":"bar"}, …]}
所以你需要json_decode
输入。然后将其传递给您的数据库:
public function store()
{
$posts = json_decode($request->input('posts', ''));
if (is_array($posts)) {
DB::table('posts')->insert($posts);
}
}
可能有一些插件或中间件或任何自动解码 JSON 有效负载的东西。但除此之外,做你所要求的并没有什么特别之处。
如果您不想使用该store()
方法(因为它已经存储了单个帖子或其他内容),您可以为多个帖子添加另一条路线。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句