Laravel - joining 2 tables with OR operator










3















I have a table called messages, and it's columns are



id, sender_id, message, receiver_id, created_at, updated_at


And the other table is called users, and it's columns are



id, firstname, lastname, email, password, created_at,updated_at


I want to get all the messages from all the users, so I'm joining the messages.sender_id and messages.receiver_id with users.id



However I don't know how to use "OR" operator while joining in laravel
here's my raw query that gives me the desired output.



SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
OR users.id = messages.receiver_id GROUP BY messages.id


Here's the laravel query that I tried



$get_messages = DB::table('messages')
->where('messages.sender_id',$user_id)
->leftjoin('users', function($join)
$join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
$join->on('users.id','=','messages.receiver_id');
)
->orwhere('messages.sender_id',$partner_id)
->where('messages.receiver_id',$user_id)
->orwhere('messages.receiver_id',$partner_id)
->groupby('messages.id')
->get();

print_r($get_messages);









share|improve this question


























    3















    I have a table called messages, and it's columns are



    id, sender_id, message, receiver_id, created_at, updated_at


    And the other table is called users, and it's columns are



    id, firstname, lastname, email, password, created_at,updated_at


    I want to get all the messages from all the users, so I'm joining the messages.sender_id and messages.receiver_id with users.id



    However I don't know how to use "OR" operator while joining in laravel
    here's my raw query that gives me the desired output.



    SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
    OR users.id = messages.receiver_id GROUP BY messages.id


    Here's the laravel query that I tried



    $get_messages = DB::table('messages')
    ->where('messages.sender_id',$user_id)
    ->leftjoin('users', function($join)
    $join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
    $join->on('users.id','=','messages.receiver_id');
    )
    ->orwhere('messages.sender_id',$partner_id)
    ->where('messages.receiver_id',$user_id)
    ->orwhere('messages.receiver_id',$partner_id)
    ->groupby('messages.id')
    ->get();

    print_r($get_messages);









    share|improve this question
























      3












      3








      3


      2






      I have a table called messages, and it's columns are



      id, sender_id, message, receiver_id, created_at, updated_at


      And the other table is called users, and it's columns are



      id, firstname, lastname, email, password, created_at,updated_at


      I want to get all the messages from all the users, so I'm joining the messages.sender_id and messages.receiver_id with users.id



      However I don't know how to use "OR" operator while joining in laravel
      here's my raw query that gives me the desired output.



      SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
      OR users.id = messages.receiver_id GROUP BY messages.id


      Here's the laravel query that I tried



      $get_messages = DB::table('messages')
      ->where('messages.sender_id',$user_id)
      ->leftjoin('users', function($join)
      $join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
      $join->on('users.id','=','messages.receiver_id');
      )
      ->orwhere('messages.sender_id',$partner_id)
      ->where('messages.receiver_id',$user_id)
      ->orwhere('messages.receiver_id',$partner_id)
      ->groupby('messages.id')
      ->get();

      print_r($get_messages);









      share|improve this question














      I have a table called messages, and it's columns are



      id, sender_id, message, receiver_id, created_at, updated_at


      And the other table is called users, and it's columns are



      id, firstname, lastname, email, password, created_at,updated_at


      I want to get all the messages from all the users, so I'm joining the messages.sender_id and messages.receiver_id with users.id



      However I don't know how to use "OR" operator while joining in laravel
      here's my raw query that gives me the desired output.



      SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
      OR users.id = messages.receiver_id GROUP BY messages.id


      Here's the laravel query that I tried



      $get_messages = DB::table('messages')
      ->where('messages.sender_id',$user_id)
      ->leftjoin('users', function($join)
      $join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
      $join->on('users.id','=','messages.receiver_id');
      )
      ->orwhere('messages.sender_id',$partner_id)
      ->where('messages.receiver_id',$user_id)
      ->orwhere('messages.receiver_id',$partner_id)
      ->groupby('messages.id')
      ->get();

      print_r($get_messages);






      php mysql laravel laravel-5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 18 '16 at 2:03









      KennethCKennethC

      281215




      281215






















          2 Answers
          2






          active

          oldest

          votes


















          10














          Try this.



           ->leftjoin('users', function($join)
          $join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
          $join->orOn('users.id','=','messages.receiver_id');
          )





          share|improve this answer























          • Thanks brother. It works fine.

            – KennethC
            Mar 18 '16 at 2:09












          • @KennethC, please accept the answer if it solved your case. Thanks :)

            – aadarshsg
            Mar 18 '16 at 6:34











          • done bro. thanks

            – KennethC
            Mar 19 '16 at 3:51











          • thanks Bro. its works really well

            – thumber nirmal
            Oct 1 '18 at 10:34


















          0














          YOu can also use the direct query



          $results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
          OR users.id = messages.receiver_id GROUP BY messages.id") );





          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%2f36074866%2flaravel-joining-2-tables-with-or-operator%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









            10














            Try this.



             ->leftjoin('users', function($join)
            $join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
            $join->orOn('users.id','=','messages.receiver_id');
            )





            share|improve this answer























            • Thanks brother. It works fine.

              – KennethC
              Mar 18 '16 at 2:09












            • @KennethC, please accept the answer if it solved your case. Thanks :)

              – aadarshsg
              Mar 18 '16 at 6:34











            • done bro. thanks

              – KennethC
              Mar 19 '16 at 3:51











            • thanks Bro. its works really well

              – thumber nirmal
              Oct 1 '18 at 10:34















            10














            Try this.



             ->leftjoin('users', function($join)
            $join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
            $join->orOn('users.id','=','messages.receiver_id');
            )





            share|improve this answer























            • Thanks brother. It works fine.

              – KennethC
              Mar 18 '16 at 2:09












            • @KennethC, please accept the answer if it solved your case. Thanks :)

              – aadarshsg
              Mar 18 '16 at 6:34











            • done bro. thanks

              – KennethC
              Mar 19 '16 at 3:51











            • thanks Bro. its works really well

              – thumber nirmal
              Oct 1 '18 at 10:34













            10












            10








            10







            Try this.



             ->leftjoin('users', function($join)
            $join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
            $join->orOn('users.id','=','messages.receiver_id');
            )





            share|improve this answer













            Try this.



             ->leftjoin('users', function($join)
            $join->on('users.id','=','messages.sender_id'); // i want to join the users table with either of these columns
            $join->orOn('users.id','=','messages.receiver_id');
            )






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 18 '16 at 2:07









            aadarshsgaadarshsg

            1,6321024




            1,6321024












            • Thanks brother. It works fine.

              – KennethC
              Mar 18 '16 at 2:09












            • @KennethC, please accept the answer if it solved your case. Thanks :)

              – aadarshsg
              Mar 18 '16 at 6:34











            • done bro. thanks

              – KennethC
              Mar 19 '16 at 3:51











            • thanks Bro. its works really well

              – thumber nirmal
              Oct 1 '18 at 10:34

















            • Thanks brother. It works fine.

              – KennethC
              Mar 18 '16 at 2:09












            • @KennethC, please accept the answer if it solved your case. Thanks :)

              – aadarshsg
              Mar 18 '16 at 6:34











            • done bro. thanks

              – KennethC
              Mar 19 '16 at 3:51











            • thanks Bro. its works really well

              – thumber nirmal
              Oct 1 '18 at 10:34
















            Thanks brother. It works fine.

            – KennethC
            Mar 18 '16 at 2:09






            Thanks brother. It works fine.

            – KennethC
            Mar 18 '16 at 2:09














            @KennethC, please accept the answer if it solved your case. Thanks :)

            – aadarshsg
            Mar 18 '16 at 6:34





            @KennethC, please accept the answer if it solved your case. Thanks :)

            – aadarshsg
            Mar 18 '16 at 6:34













            done bro. thanks

            – KennethC
            Mar 19 '16 at 3:51





            done bro. thanks

            – KennethC
            Mar 19 '16 at 3:51













            thanks Bro. its works really well

            – thumber nirmal
            Oct 1 '18 at 10:34





            thanks Bro. its works really well

            – thumber nirmal
            Oct 1 '18 at 10:34













            0














            YOu can also use the direct query



            $results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
            OR users.id = messages.receiver_id GROUP BY messages.id") );





            share|improve this answer



























              0














              YOu can also use the direct query



              $results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
              OR users.id = messages.receiver_id GROUP BY messages.id") );





              share|improve this answer

























                0












                0








                0







                YOu can also use the direct query



                $results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
                OR users.id = messages.receiver_id GROUP BY messages.id") );





                share|improve this answer













                YOu can also use the direct query



                $results = DB::select( DB::raw("SELECT messages.*,users.* FROM messages JOIN users ON users.id = messages.sender_id 
                OR users.id = messages.receiver_id GROUP BY messages.id") );






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 18 '16 at 4:25









                Haseena P AHaseena P A

                4,52531025




                4,52531025



























                    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%2f36074866%2flaravel-joining-2-tables-with-or-operator%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

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo