Many-to-many relationship - it doesn't display elements












1















I want to have many-to-many relationship. So i created:
Into Game model



public function category(){
return $this->belongsToMany('AppCategory');
}


Into Category model



public function games(){
return $this->hasMany('AppGame');
}


Into controller



$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());


Generally, I try display all games belongs to particular category. I see something like this
enter image description here



If I remove dd() view won't display any elements.But it doesn't problem with view.



@foreach($elements as $element)
//...
@endforeach


Why it doesn;t work?










share|improve this question























  • It must be $category->games not $category->games(), Call it like a property.

    – ako
    Nov 20 '18 at 13:56


















1















I want to have many-to-many relationship. So i created:
Into Game model



public function category(){
return $this->belongsToMany('AppCategory');
}


Into Category model



public function games(){
return $this->hasMany('AppGame');
}


Into controller



$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());


Generally, I try display all games belongs to particular category. I see something like this
enter image description here



If I remove dd() view won't display any elements.But it doesn't problem with view.



@foreach($elements as $element)
//...
@endforeach


Why it doesn;t work?










share|improve this question























  • It must be $category->games not $category->games(), Call it like a property.

    – ako
    Nov 20 '18 at 13:56
















1












1








1








I want to have many-to-many relationship. So i created:
Into Game model



public function category(){
return $this->belongsToMany('AppCategory');
}


Into Category model



public function games(){
return $this->hasMany('AppGame');
}


Into controller



$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());


Generally, I try display all games belongs to particular category. I see something like this
enter image description here



If I remove dd() view won't display any elements.But it doesn't problem with view.



@foreach($elements as $element)
//...
@endforeach


Why it doesn;t work?










share|improve this question














I want to have many-to-many relationship. So i created:
Into Game model



public function category(){
return $this->belongsToMany('AppCategory');
}


Into Category model



public function games(){
return $this->hasMany('AppGame');
}


Into controller



$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());


Generally, I try display all games belongs to particular category. I see something like this
enter image description here



If I remove dd() view won't display any elements.But it doesn't problem with view.



@foreach($elements as $element)
//...
@endforeach


Why it doesn;t work?







laravel many-to-many relationship






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 13:49









MrBelongsToMrBelongsTo

186




186













  • It must be $category->games not $category->games(), Call it like a property.

    – ako
    Nov 20 '18 at 13:56





















  • It must be $category->games not $category->games(), Call it like a property.

    – ako
    Nov 20 '18 at 13:56



















It must be $category->games not $category->games(), Call it like a property.

– ako
Nov 20 '18 at 13:56







It must be $category->games not $category->games(), Call it like a property.

– ako
Nov 20 '18 at 13:56














4 Answers
4






active

oldest

votes


















0














You Category model also needs to have belongsToMany in its games() relation.



public function games() {
return $this->belongsToMany('AppGame');
}





share|improve this answer































    0














    You are using lazy loaded data



    Laravel eloquent Lazy Vs. Eager Loaded



    Update your controller method:



    $category = Category::with('games')->where('slug', $slug)->first();
    dd($category->games);

    return view('frontend.game.gamelist')->with('elements', $category);


    You have to get all the category with games, so that you can get those data with an query also see them in dd function.



    If you only get category and then do category->games() it will execute another query in your view.






    share|improve this answer































      0
















      When you do:



      $category->games();


      you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.



      Try this instead:



      $category->games;




      OBS



      When doing a Many-to-Many relationship, the belongsToMany method needs to be specified in both models.



      Category.php



      public function games()
      {
      return $this->belongsToMany('AppGame');
      }





      share|improve this answer































        0














        You need to have a belongsToMany relationship in your Category model as well to have a many-many relationship. You can achieve this by doing:



        public function games() 
        {
        return $this->belongsToMany('AppGame');
        }


        Then in your category controller, you have:



        $category = Category::with('games')->where('slug', $slug)->get();

        return view('frontend.game.gamelist', compact('category');


        Then in your view, you can access the list of games by



        @foreach($category->games as $game)
        //...
        @endforeach





        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394478%2fmany-to-many-relationship-it-doesnt-display-elements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You Category model also needs to have belongsToMany in its games() relation.



          public function games() {
          return $this->belongsToMany('AppGame');
          }





          share|improve this answer




























            0














            You Category model also needs to have belongsToMany in its games() relation.



            public function games() {
            return $this->belongsToMany('AppGame');
            }





            share|improve this answer


























              0












              0








              0







              You Category model also needs to have belongsToMany in its games() relation.



              public function games() {
              return $this->belongsToMany('AppGame');
              }





              share|improve this answer













              You Category model also needs to have belongsToMany in its games() relation.



              public function games() {
              return $this->belongsToMany('AppGame');
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 20 '18 at 13:51









              JerodevJerodev

              19k84572




              19k84572

























                  0














                  You are using lazy loaded data



                  Laravel eloquent Lazy Vs. Eager Loaded



                  Update your controller method:



                  $category = Category::with('games')->where('slug', $slug)->first();
                  dd($category->games);

                  return view('frontend.game.gamelist')->with('elements', $category);


                  You have to get all the category with games, so that you can get those data with an query also see them in dd function.



                  If you only get category and then do category->games() it will execute another query in your view.






                  share|improve this answer




























                    0














                    You are using lazy loaded data



                    Laravel eloquent Lazy Vs. Eager Loaded



                    Update your controller method:



                    $category = Category::with('games')->where('slug', $slug)->first();
                    dd($category->games);

                    return view('frontend.game.gamelist')->with('elements', $category);


                    You have to get all the category with games, so that you can get those data with an query also see them in dd function.



                    If you only get category and then do category->games() it will execute another query in your view.






                    share|improve this answer


























                      0












                      0








                      0







                      You are using lazy loaded data



                      Laravel eloquent Lazy Vs. Eager Loaded



                      Update your controller method:



                      $category = Category::with('games')->where('slug', $slug)->first();
                      dd($category->games);

                      return view('frontend.game.gamelist')->with('elements', $category);


                      You have to get all the category with games, so that you can get those data with an query also see them in dd function.



                      If you only get category and then do category->games() it will execute another query in your view.






                      share|improve this answer













                      You are using lazy loaded data



                      Laravel eloquent Lazy Vs. Eager Loaded



                      Update your controller method:



                      $category = Category::with('games')->where('slug', $slug)->first();
                      dd($category->games);

                      return view('frontend.game.gamelist')->with('elements', $category);


                      You have to get all the category with games, so that you can get those data with an query also see them in dd function.



                      If you only get category and then do category->games() it will execute another query in your view.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 20 '18 at 13:56









                      Emtiaz ZahidEmtiaz Zahid

                      1,043616




                      1,043616























                          0
















                          When you do:



                          $category->games();


                          you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.



                          Try this instead:



                          $category->games;




                          OBS



                          When doing a Many-to-Many relationship, the belongsToMany method needs to be specified in both models.



                          Category.php



                          public function games()
                          {
                          return $this->belongsToMany('AppGame');
                          }





                          share|improve this answer




























                            0
















                            When you do:



                            $category->games();


                            you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.



                            Try this instead:



                            $category->games;




                            OBS



                            When doing a Many-to-Many relationship, the belongsToMany method needs to be specified in both models.



                            Category.php



                            public function games()
                            {
                            return $this->belongsToMany('AppGame');
                            }





                            share|improve this answer


























                              0












                              0








                              0









                              When you do:



                              $category->games();


                              you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.



                              Try this instead:



                              $category->games;




                              OBS



                              When doing a Many-to-Many relationship, the belongsToMany method needs to be specified in both models.



                              Category.php



                              public function games()
                              {
                              return $this->belongsToMany('AppGame');
                              }





                              share|improve this answer















                              When you do:



                              $category->games();


                              you are accessing the relationship itself (useful to attach contraints), not the elements of the relation. Check this other answer for a detail explanation.



                              Try this instead:



                              $category->games;




                              OBS



                              When doing a Many-to-Many relationship, the belongsToMany method needs to be specified in both models.



                              Category.php



                              public function games()
                              {
                              return $this->belongsToMany('AppGame');
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 20 '18 at 13:58









                              HCKHCK

                              3,58911035




                              3,58911035























                                  0














                                  You need to have a belongsToMany relationship in your Category model as well to have a many-many relationship. You can achieve this by doing:



                                  public function games() 
                                  {
                                  return $this->belongsToMany('AppGame');
                                  }


                                  Then in your category controller, you have:



                                  $category = Category::with('games')->where('slug', $slug)->get();

                                  return view('frontend.game.gamelist', compact('category');


                                  Then in your view, you can access the list of games by



                                  @foreach($category->games as $game)
                                  //...
                                  @endforeach





                                  share|improve this answer






























                                    0














                                    You need to have a belongsToMany relationship in your Category model as well to have a many-many relationship. You can achieve this by doing:



                                    public function games() 
                                    {
                                    return $this->belongsToMany('AppGame');
                                    }


                                    Then in your category controller, you have:



                                    $category = Category::with('games')->where('slug', $slug)->get();

                                    return view('frontend.game.gamelist', compact('category');


                                    Then in your view, you can access the list of games by



                                    @foreach($category->games as $game)
                                    //...
                                    @endforeach





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      You need to have a belongsToMany relationship in your Category model as well to have a many-many relationship. You can achieve this by doing:



                                      public function games() 
                                      {
                                      return $this->belongsToMany('AppGame');
                                      }


                                      Then in your category controller, you have:



                                      $category = Category::with('games')->where('slug', $slug)->get();

                                      return view('frontend.game.gamelist', compact('category');


                                      Then in your view, you can access the list of games by



                                      @foreach($category->games as $game)
                                      //...
                                      @endforeach





                                      share|improve this answer















                                      You need to have a belongsToMany relationship in your Category model as well to have a many-many relationship. You can achieve this by doing:



                                      public function games() 
                                      {
                                      return $this->belongsToMany('AppGame');
                                      }


                                      Then in your category controller, you have:



                                      $category = Category::with('games')->where('slug', $slug)->get();

                                      return view('frontend.game.gamelist', compact('category');


                                      Then in your view, you can access the list of games by



                                      @foreach($category->games as $game)
                                      //...
                                      @endforeach






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 20 '18 at 14:10

























                                      answered Nov 20 '18 at 14:04









                                      Peter SowahPeter Sowah

                                      445211




                                      445211






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394478%2fmany-to-many-relationship-it-doesnt-display-elements%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          How to change which sound is reproduced for terminal bell?

                                          Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents