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

Time:03-25

I am creating a movies api where movies can be show caterywise.

For that I have two tables, one is movies with cat_id as foreign key and the second is categories with cat_id as a primary key. with below fields

enter image description here enter image description here

and I have returned the API like this of movies

enter image description herefurther, you may visit this link https://backend.hac-inc.org/api/movies

but want to convert it like to get movies within the object of its categories as below

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"
    }]
}

I have used has many functions in the collection but it didn't help. Please someone help me, I am badly stuck.

Trying to convert my simple movies table API to show them in a group of categories.

CodePudding user response:

You could just make a relationship inside Category models to get the list of categories and inside each of the categories, there will be movies property that consists of the movies related to the category.

Add this to your Category model:

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

Then to get the movies property, you can use this:

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

CodePudding user response:

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

I just wrote this and it worked.

  • Related