How to make Child table to access another laravel table

I am using Laravel 5.1.

I have three tables.

  • Films
  • Video
  • Video category

The user can create a movie and add many videos, but each video must belong to any category (e.g. video, trailer, movie events).

Now I want to get all movies, videos and video categories of each video by passing the movie id.

Table structure

Movies table

filmID | filmName 
-----------------
   1   |  Film 1
   2   |  Film 2

      

Video table

videoID | filmId | catID | videoURL
-----------------------------------
   1    |    1   |  cat1 |   URL 1
   2    |    1   |  cat2 |   URL 2

      

Video Category Table

  catID  | category
--------------------
   cat1  |  Trailer
   cat2  |  Making
   cat3  |  Events

      

I can get all videos by passing FilmID

{
  filmId: "filmName",
  filmName: "Film Name",
  coverPhoto: "image URL",
  created_at: "2015-06-28 06:35:26",
  updated_at: "2015-06-28 06:35:45",
  videos: [
      {
        id: 7,
        filmId: "filmID",
        videoCategory: "trailer",
        videoUrl: "h41Jb29P4",
        created_at: "2015-06-28 11:40:22",
        updated_at: "2015-06-28 11:40:22"
      }
  ]
}

      

But how to get the video category together with this output. Something like that

{
      filmId: "filmName",
      filmName: "Film Name",
      coverPhoto: "image URL",
      created_at: "2015-06-28 06:35:26",
      updated_at: "2015-06-28 06:35:45",
      videos: [
          {
            id: 7,
            filmId: "filmID",
            videoCategory: "trailer",
            videoUrl: "h41Jb29P4",
            created_at: "2015-06-28 11:40:22",
            updated_at: "2015-06-28 11:40:22"
            videoCategory: [
                  {
                     videoCategoryId: "1",
                     videoCategoryName: "Trailer"
                  }
            ]
          }
      ]
    }

      

Is there a way to get around without passing multiple requests and merging in PHP?

Please help me.

+3


source to share


1 answer


Yes, I found the answer myself. I used the concept of loading Nested Eager in Laravel



Laravel 5 Eager Loading does the job

0


source







All Articles