如何使用 Laravel 控制器将对象放置在 JSON Api 中的对象内?

How to place object within object in JSON Api using Laravel controller?

我正在制作电影 api 可以按类别放映电影。

为此我有两个 tables,一个是 cat_id 作为外键的电影,第二个是 的类别cat_id 作为主键。 具有以下字段

我还了API这样的电影

另外,你可以访问这个linkhttps://backend.hac-inc.org/api/movies

但想要将其转换为在其类别的对象中获取电影,如下所示

enter image description here

{
"movies":[
        "Action":[{
      "id": 1,
      "title": "Attack",
      "description": "Attack _ Official Trailer _ John A, Jacqueline F, Rakul Preet S _ Lakshya Raj Anand_ April 1st, 2022",
      "cast": "John A, Jacqueline F, Rakul Preet S _ Lakshya Raj Anand",
      "language": "Hindi",
      "category": "Action",
      "movie_path": "https://backend.hac-inc.org/images/movies/1647589765.jpg",
      "movie_url": "https://backend.hac-inc.org/movies/1647589765.mp4",
      "movie_image_full_name": "1647589765.jpg",
      "movie_file_full_name": "1647589765.mp4",
      "created_at": "2022-03-17T22:49:25.000000Z",
      "updated_at": "2022-03-17T22:49:25.000000Z"
    },
    {
      "id": 2,
      "title": "Bachchhan Paandey",
      "description": "Bachchhan Paandey _ Official Trailer _ Akshay Kriti Jacqueline Arshad _ Sajid N _Farhad S_18th March",
      "cast": "Akshay Kriti Jacqueline Arshad",
      "language": "Hindi",
      "category": "Action",
      "movie_path": "https://backend.hac-inc.org/images/movies/1647596446.jpg",
      "movie_url": "https://backend.hac-inc.org/movies/1647596446.mp4",
      "movie_image_full_name": "1647596446.jpg",
      "movie_file_full_name": "1647596446.mp4",
      "created_at": "2022-03-18T00:40:46.000000Z",
      "updated_at": "2022-03-18T00:40:46.000000Z"
    },{
      "id": 5,
      "title": "Rudra",
      "description": "Rudra _ Official Trailer _ Coming Soon _ DisneyPlus Hotstar",
      "cast": "Ajay Devgan",
      "language": "Hindi",
      "category": "Action",
      "movie_path": "https://backend.hac-inc.org/images/movies/1647596619.jpg",
      "movie_url": "https://backend.hac-inc.org/movies/1647596619.mp4",
      "movie_image_full_name": "1647596619.jpg",
      "movie_file_full_name": "1647596619.mp4",
      "created_at": "2022-03-18T00:43:39.000000Z",
      "updated_at": "2022-03-18T00:43:39.000000Z"
    }
    ],
    "Romantic":[{
      "id": 4,
      "title": "October",
      "description": "October _ Official Trailer _ Varun Dhawan _ Banita Sandhu _ Shoojit Sircar",
      "cast": "Varun Dhawan _ Banita Sandhu _ Shoojit Sircar",
      "language": "Hindi",
      "category": "Romantic",
      "movie_path": "https://backend.hac-inc.org/images/movies/1647596567.jpg",
      "movie_url": "https://backend.hac-inc.org/movies/1647596567.mp4",
      "movie_image_full_name": "1647596567.jpg",
      "movie_file_full_name": "1647596567.mp4",
      "created_at": "2022-03-18T00:42:47.000000Z",
      "updated_at": "2022-03-18T00:42:47.000000Z"
    }]
}

我已经使用了集合中的许多功能,但没有帮助。请有人帮助我,我被严重卡住了。

正在尝试转换我的简单电影 table API 以在一组类别中显示它们。

您可以在类别模型中建立关系以获取类别列表,在每个类别中,将有电影 属性 包含与该类别相关的电影。

将此添加到您的类别模型中:

public function movies()
{
    return $this->hasMany(Movie::class, 'cat_id', 'id');
}

然后要获取电影 属性,您可以使用此:

return $categories->with(['movies']);

$movies = Movie::all();
return $movies->groupBy('category');

我刚刚写了这个并且它起作用了。