How with, where operators work in Laravel?












0















How do with, where eloquent operators work in Laravel?



Is it possible to see is build result SQL query after using with, where or these operators applied only for result data model?



Does with(<related model>) operator work as INNER JOIN?



Where to explore information about that?










share|improve this question























  • Try using a good ide like PHPStorm and following the method to understand how it works.

    – Elias Soares
    Nov 14 '18 at 0:52
















0















How do with, where eloquent operators work in Laravel?



Is it possible to see is build result SQL query after using with, where or these operators applied only for result data model?



Does with(<related model>) operator work as INNER JOIN?



Where to explore information about that?










share|improve this question























  • Try using a good ide like PHPStorm and following the method to understand how it works.

    – Elias Soares
    Nov 14 '18 at 0:52














0












0








0


1






How do with, where eloquent operators work in Laravel?



Is it possible to see is build result SQL query after using with, where or these operators applied only for result data model?



Does with(<related model>) operator work as INNER JOIN?



Where to explore information about that?










share|improve this question














How do with, where eloquent operators work in Laravel?



Is it possible to see is build result SQL query after using with, where or these operators applied only for result data model?



Does with(<related model>) operator work as INNER JOIN?



Where to explore information about that?







laravel laravel-5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 0:40









OPVOPV

1,57721337




1,57721337













  • Try using a good ide like PHPStorm and following the method to understand how it works.

    – Elias Soares
    Nov 14 '18 at 0:52



















  • Try using a good ide like PHPStorm and following the method to understand how it works.

    – Elias Soares
    Nov 14 '18 at 0:52

















Try using a good ide like PHPStorm and following the method to understand how it works.

– Elias Soares
Nov 14 '18 at 0:52





Try using a good ide like PHPStorm and following the method to understand how it works.

– Elias Soares
Nov 14 '18 at 0:52












4 Answers
4






active

oldest

votes


















3
















To know the SQL Query that Laravel execute you can use the toSql() function:



$results = User::where(function($q) use ($request) {
$q->orWhere('email', 'like', '%john@example.org%');
$q->orWhere('first_name', 'like', '%John%');
$q->orWhere('last_name', 'like', '%Doe%');
})->toSql();

dd($results); // output


Check this article for more information regarding this aspect. There is also another alternative to this approach.



Regarding your next question: No, Laravel doesn't do JOINs. You can read a little bit about this regard in this article.





PD: If you want to know more about how Eloquent work, you can follow this good Laracasts series.






share|improve this answer

































    1














    Since using with() will result in multiple SQL statements being ran, a good way to understand the underlying SQL is to use DB::enableQueryLog() and DB::getQueryLog().






    share|improve this answer































      1
















      According to SQL above you want to find Product with the name $needle or manufacturer name $needle. Guess You need whereHas Method.



      $products = Product::with("manufacturer")
      ->whereHas('manufacturer',function($query) use ($needle){
      $query->where("name","like","%{$needle}%");
      })
      ->orWhere("name","like","%{$needle}%")->get();


      You can also prepend it to "with" method with the same query condition to obtain the relation instance it matched. In case of belongsTo you can use it without specifying condition.



      Check out querying relations: https://laravel.com/docs/5.5/eloquent-relationships#querying-relations






      share|improve this answer

































        1














        There are two ways to get detailed info regarding with and where condition:
        - use Laravel debug bar in the development environment to see detailed info regarding queries. What's the structure, frequencies of queries etc.
        - Use toSql function to dump query






        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%2f53291535%2fhow-with-where-operators-work-in-laravel%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









          3
















          To know the SQL Query that Laravel execute you can use the toSql() function:



          $results = User::where(function($q) use ($request) {
          $q->orWhere('email', 'like', '%john@example.org%');
          $q->orWhere('first_name', 'like', '%John%');
          $q->orWhere('last_name', 'like', '%Doe%');
          })->toSql();

          dd($results); // output


          Check this article for more information regarding this aspect. There is also another alternative to this approach.



          Regarding your next question: No, Laravel doesn't do JOINs. You can read a little bit about this regard in this article.





          PD: If you want to know more about how Eloquent work, you can follow this good Laracasts series.






          share|improve this answer






























            3
















            To know the SQL Query that Laravel execute you can use the toSql() function:



            $results = User::where(function($q) use ($request) {
            $q->orWhere('email', 'like', '%john@example.org%');
            $q->orWhere('first_name', 'like', '%John%');
            $q->orWhere('last_name', 'like', '%Doe%');
            })->toSql();

            dd($results); // output


            Check this article for more information regarding this aspect. There is also another alternative to this approach.



            Regarding your next question: No, Laravel doesn't do JOINs. You can read a little bit about this regard in this article.





            PD: If you want to know more about how Eloquent work, you can follow this good Laracasts series.






            share|improve this answer




























              3












              3








              3









              To know the SQL Query that Laravel execute you can use the toSql() function:



              $results = User::where(function($q) use ($request) {
              $q->orWhere('email', 'like', '%john@example.org%');
              $q->orWhere('first_name', 'like', '%John%');
              $q->orWhere('last_name', 'like', '%Doe%');
              })->toSql();

              dd($results); // output


              Check this article for more information regarding this aspect. There is also another alternative to this approach.



              Regarding your next question: No, Laravel doesn't do JOINs. You can read a little bit about this regard in this article.





              PD: If you want to know more about how Eloquent work, you can follow this good Laracasts series.






              share|improve this answer

















              To know the SQL Query that Laravel execute you can use the toSql() function:



              $results = User::where(function($q) use ($request) {
              $q->orWhere('email', 'like', '%john@example.org%');
              $q->orWhere('first_name', 'like', '%John%');
              $q->orWhere('last_name', 'like', '%Doe%');
              })->toSql();

              dd($results); // output


              Check this article for more information regarding this aspect. There is also another alternative to this approach.



              Regarding your next question: No, Laravel doesn't do JOINs. You can read a little bit about this regard in this article.





              PD: If you want to know more about how Eloquent work, you can follow this good Laracasts series.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 14 '18 at 0:57

























              answered Nov 14 '18 at 0:51









              HCKHCK

              3,50411035




              3,50411035

























                  1














                  Since using with() will result in multiple SQL statements being ran, a good way to understand the underlying SQL is to use DB::enableQueryLog() and DB::getQueryLog().






                  share|improve this answer




























                    1














                    Since using with() will result in multiple SQL statements being ran, a good way to understand the underlying SQL is to use DB::enableQueryLog() and DB::getQueryLog().






                    share|improve this answer


























                      1












                      1








                      1







                      Since using with() will result in multiple SQL statements being ran, a good way to understand the underlying SQL is to use DB::enableQueryLog() and DB::getQueryLog().






                      share|improve this answer













                      Since using with() will result in multiple SQL statements being ran, a good way to understand the underlying SQL is to use DB::enableQueryLog() and DB::getQueryLog().







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 14 '18 at 0:58









                      bradforbesbradforbes

                      39617




                      39617























                          1
















                          According to SQL above you want to find Product with the name $needle or manufacturer name $needle. Guess You need whereHas Method.



                          $products = Product::with("manufacturer")
                          ->whereHas('manufacturer',function($query) use ($needle){
                          $query->where("name","like","%{$needle}%");
                          })
                          ->orWhere("name","like","%{$needle}%")->get();


                          You can also prepend it to "with" method with the same query condition to obtain the relation instance it matched. In case of belongsTo you can use it without specifying condition.



                          Check out querying relations: https://laravel.com/docs/5.5/eloquent-relationships#querying-relations






                          share|improve this answer






























                            1
















                            According to SQL above you want to find Product with the name $needle or manufacturer name $needle. Guess You need whereHas Method.



                            $products = Product::with("manufacturer")
                            ->whereHas('manufacturer',function($query) use ($needle){
                            $query->where("name","like","%{$needle}%");
                            })
                            ->orWhere("name","like","%{$needle}%")->get();


                            You can also prepend it to "with" method with the same query condition to obtain the relation instance it matched. In case of belongsTo you can use it without specifying condition.



                            Check out querying relations: https://laravel.com/docs/5.5/eloquent-relationships#querying-relations






                            share|improve this answer




























                              1












                              1








                              1









                              According to SQL above you want to find Product with the name $needle or manufacturer name $needle. Guess You need whereHas Method.



                              $products = Product::with("manufacturer")
                              ->whereHas('manufacturer',function($query) use ($needle){
                              $query->where("name","like","%{$needle}%");
                              })
                              ->orWhere("name","like","%{$needle}%")->get();


                              You can also prepend it to "with" method with the same query condition to obtain the relation instance it matched. In case of belongsTo you can use it without specifying condition.



                              Check out querying relations: https://laravel.com/docs/5.5/eloquent-relationships#querying-relations






                              share|improve this answer

















                              According to SQL above you want to find Product with the name $needle or manufacturer name $needle. Guess You need whereHas Method.



                              $products = Product::with("manufacturer")
                              ->whereHas('manufacturer',function($query) use ($needle){
                              $query->where("name","like","%{$needle}%");
                              })
                              ->orWhere("name","like","%{$needle}%")->get();


                              You can also prepend it to "with" method with the same query condition to obtain the relation instance it matched. In case of belongsTo you can use it without specifying condition.



                              Check out querying relations: https://laravel.com/docs/5.5/eloquent-relationships#querying-relations







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 14 '18 at 14:47









                              HCK

                              3,50411035




                              3,50411035










                              answered Nov 14 '18 at 3:21









                              Ismoil ShifoevIsmoil Shifoev

                              1,485812




                              1,485812























                                  1














                                  There are two ways to get detailed info regarding with and where condition:
                                  - use Laravel debug bar in the development environment to see detailed info regarding queries. What's the structure, frequencies of queries etc.
                                  - Use toSql function to dump query






                                  share|improve this answer




























                                    1














                                    There are two ways to get detailed info regarding with and where condition:
                                    - use Laravel debug bar in the development environment to see detailed info regarding queries. What's the structure, frequencies of queries etc.
                                    - Use toSql function to dump query






                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      There are two ways to get detailed info regarding with and where condition:
                                      - use Laravel debug bar in the development environment to see detailed info regarding queries. What's the structure, frequencies of queries etc.
                                      - Use toSql function to dump query






                                      share|improve this answer













                                      There are two ways to get detailed info regarding with and where condition:
                                      - use Laravel debug bar in the development environment to see detailed info regarding queries. What's the structure, frequencies of queries etc.
                                      - Use toSql function to dump query







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 14 '18 at 15:29









                                      Software developerSoftware developer

                                      313




                                      313






























                                          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%2f53291535%2fhow-with-where-operators-work-in-laravel%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

                                          Florida Star v. B. J. F.

                                          Danny Elfman

                                          Lugert, Oklahoma