Laravel Eloquent - Pivot Table











up vote
0
down vote

favorite












i have this tables:



Attendances
id | member_id
1 | 1
2 | 2
3 | 3

Members
id | name
1 | Joe
2 | Jane
3 | David

Positions
id | position_name
1 | art
2 | singer
3 | dancer

member_position
member_id | position_id
1 | 2
1 | 1
1 | 3
2 | 1
2 | 2
3 | 3


From Attendances table, how can i select all who attended that are singers using laravel eloquent?



====EDIT=====



Attendance model:



public function member()
{
return $this->belongsTo('AppMember');
}


Member model:



public function positions()
{
return $this->belongsToMany('AppPosition');
}


Position model:



public function members() 
{
return $this->belongsToMany('AppMember');
}









share|improve this question
























  • Attach your models relations
    – IndianCoding
    Nov 17 at 14:29










  • @IndianCoding - added
    – kapitan
    Nov 17 at 14:33















up vote
0
down vote

favorite












i have this tables:



Attendances
id | member_id
1 | 1
2 | 2
3 | 3

Members
id | name
1 | Joe
2 | Jane
3 | David

Positions
id | position_name
1 | art
2 | singer
3 | dancer

member_position
member_id | position_id
1 | 2
1 | 1
1 | 3
2 | 1
2 | 2
3 | 3


From Attendances table, how can i select all who attended that are singers using laravel eloquent?



====EDIT=====



Attendance model:



public function member()
{
return $this->belongsTo('AppMember');
}


Member model:



public function positions()
{
return $this->belongsToMany('AppPosition');
}


Position model:



public function members() 
{
return $this->belongsToMany('AppMember');
}









share|improve this question
























  • Attach your models relations
    – IndianCoding
    Nov 17 at 14:29










  • @IndianCoding - added
    – kapitan
    Nov 17 at 14:33













up vote
0
down vote

favorite









up vote
0
down vote

favorite











i have this tables:



Attendances
id | member_id
1 | 1
2 | 2
3 | 3

Members
id | name
1 | Joe
2 | Jane
3 | David

Positions
id | position_name
1 | art
2 | singer
3 | dancer

member_position
member_id | position_id
1 | 2
1 | 1
1 | 3
2 | 1
2 | 2
3 | 3


From Attendances table, how can i select all who attended that are singers using laravel eloquent?



====EDIT=====



Attendance model:



public function member()
{
return $this->belongsTo('AppMember');
}


Member model:



public function positions()
{
return $this->belongsToMany('AppPosition');
}


Position model:



public function members() 
{
return $this->belongsToMany('AppMember');
}









share|improve this question















i have this tables:



Attendances
id | member_id
1 | 1
2 | 2
3 | 3

Members
id | name
1 | Joe
2 | Jane
3 | David

Positions
id | position_name
1 | art
2 | singer
3 | dancer

member_position
member_id | position_id
1 | 2
1 | 1
1 | 3
2 | 1
2 | 2
3 | 3


From Attendances table, how can i select all who attended that are singers using laravel eloquent?



====EDIT=====



Attendance model:



public function member()
{
return $this->belongsTo('AppMember');
}


Member model:



public function positions()
{
return $this->belongsToMany('AppPosition');
}


Position model:



public function members() 
{
return $this->belongsToMany('AppMember');
}






laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 14:33

























asked Nov 17 at 14:18









kapitan

1389




1389












  • Attach your models relations
    – IndianCoding
    Nov 17 at 14:29










  • @IndianCoding - added
    – kapitan
    Nov 17 at 14:33


















  • Attach your models relations
    – IndianCoding
    Nov 17 at 14:29










  • @IndianCoding - added
    – kapitan
    Nov 17 at 14:33
















Attach your models relations
– IndianCoding
Nov 17 at 14:29




Attach your models relations
– IndianCoding
Nov 17 at 14:29












@IndianCoding - added
– kapitan
Nov 17 at 14:33




@IndianCoding - added
– kapitan
Nov 17 at 14:33












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You should use whereHas method with nested relations.



$attendances = Attendance::with('member')
->whereHas('member.positions', function($query){
$query->where('id', 2);
})
->get();

foreach($attendances as $attendance){
echo $attendance->member->id;
}





share|improve this answer








New contributor




IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    1
    down vote













    I'm positive this will give you the attendances whose member is a singer, with a nested member relationship model.



    $attendances = Attendance::whereHas('member.positions', function($query) {

    $query->where('position_name', 'singers');

    })->with('member')->get();


    And then, you can iterate through your results as so:



    foreach($attendances as $attendance){
    echo $attendance->member->id;
    echo $attendance->member->name;
    }





    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',
      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%2f53352069%2flaravel-eloquent-pivot-table%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      You should use whereHas method with nested relations.



      $attendances = Attendance::with('member')
      ->whereHas('member.positions', function($query){
      $query->where('id', 2);
      })
      ->get();

      foreach($attendances as $attendance){
      echo $attendance->member->id;
      }





      share|improve this answer








      New contributor




      IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        up vote
        1
        down vote



        accepted










        You should use whereHas method with nested relations.



        $attendances = Attendance::with('member')
        ->whereHas('member.positions', function($query){
        $query->where('id', 2);
        })
        ->get();

        foreach($attendances as $attendance){
        echo $attendance->member->id;
        }





        share|improve this answer








        New contributor




        IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You should use whereHas method with nested relations.



          $attendances = Attendance::with('member')
          ->whereHas('member.positions', function($query){
          $query->where('id', 2);
          })
          ->get();

          foreach($attendances as $attendance){
          echo $attendance->member->id;
          }





          share|improve this answer








          New contributor




          IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          You should use whereHas method with nested relations.



          $attendances = Attendance::with('member')
          ->whereHas('member.positions', function($query){
          $query->where('id', 2);
          })
          ->get();

          foreach($attendances as $attendance){
          echo $attendance->member->id;
          }






          share|improve this answer








          New contributor




          IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 17 at 14:41









          IndianCoding

          2967




          2967




          New contributor




          IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          IndianCoding is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.
























              up vote
              1
              down vote













              I'm positive this will give you the attendances whose member is a singer, with a nested member relationship model.



              $attendances = Attendance::whereHas('member.positions', function($query) {

              $query->where('position_name', 'singers');

              })->with('member')->get();


              And then, you can iterate through your results as so:



              foreach($attendances as $attendance){
              echo $attendance->member->id;
              echo $attendance->member->name;
              }





              share|improve this answer

























                up vote
                1
                down vote













                I'm positive this will give you the attendances whose member is a singer, with a nested member relationship model.



                $attendances = Attendance::whereHas('member.positions', function($query) {

                $query->where('position_name', 'singers');

                })->with('member')->get();


                And then, you can iterate through your results as so:



                foreach($attendances as $attendance){
                echo $attendance->member->id;
                echo $attendance->member->name;
                }





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  I'm positive this will give you the attendances whose member is a singer, with a nested member relationship model.



                  $attendances = Attendance::whereHas('member.positions', function($query) {

                  $query->where('position_name', 'singers');

                  })->with('member')->get();


                  And then, you can iterate through your results as so:



                  foreach($attendances as $attendance){
                  echo $attendance->member->id;
                  echo $attendance->member->name;
                  }





                  share|improve this answer












                  I'm positive this will give you the attendances whose member is a singer, with a nested member relationship model.



                  $attendances = Attendance::whereHas('member.positions', function($query) {

                  $query->where('position_name', 'singers');

                  })->with('member')->get();


                  And then, you can iterate through your results as so:



                  foreach($attendances as $attendance){
                  echo $attendance->member->id;
                  echo $attendance->member->name;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 at 14:50









                  Oniya Daniel

                  198213




                  198213






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352069%2flaravel-eloquent-pivot-table%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

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin