How can i make inner join in method instead of controller?











up vote
0
down vote

favorite












I have a two tables orders and product. Relation is One to One. In my order class i created method product



public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id');
}


Now in controller i want select rows where id is not null that's why i wanna use InnerJoin



Order::where('order_id', 7)
->join('products', 'products.key_id', 'orders.key_id')
->get();


That is fine but i would like get



Order::where('order_id', 7)->get();


and all join move to method like:



public function product() {
return $this->hasOne('AppProduct', 'key_id', 'key_id')
->join('products', 'products.key_id', 'orders.key_id')
}


How can i do this ?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a two tables orders and product. Relation is One to One. In my order class i created method product



    public function product() {
    return $this->hasOne('AppProduct', 'key_id', 'key_id');
    }


    Now in controller i want select rows where id is not null that's why i wanna use InnerJoin



    Order::where('order_id', 7)
    ->join('products', 'products.key_id', 'orders.key_id')
    ->get();


    That is fine but i would like get



    Order::where('order_id', 7)->get();


    and all join move to method like:



    public function product() {
    return $this->hasOne('AppProduct', 'key_id', 'key_id')
    ->join('products', 'products.key_id', 'orders.key_id')
    }


    How can i do this ?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a two tables orders and product. Relation is One to One. In my order class i created method product



      public function product() {
      return $this->hasOne('AppProduct', 'key_id', 'key_id');
      }


      Now in controller i want select rows where id is not null that's why i wanna use InnerJoin



      Order::where('order_id', 7)
      ->join('products', 'products.key_id', 'orders.key_id')
      ->get();


      That is fine but i would like get



      Order::where('order_id', 7)->get();


      and all join move to method like:



      public function product() {
      return $this->hasOne('AppProduct', 'key_id', 'key_id')
      ->join('products', 'products.key_id', 'orders.key_id')
      }


      How can i do this ?










      share|improve this question













      I have a two tables orders and product. Relation is One to One. In my order class i created method product



      public function product() {
      return $this->hasOne('AppProduct', 'key_id', 'key_id');
      }


      Now in controller i want select rows where id is not null that's why i wanna use InnerJoin



      Order::where('order_id', 7)
      ->join('products', 'products.key_id', 'orders.key_id')
      ->get();


      That is fine but i would like get



      Order::where('order_id', 7)->get();


      and all join move to method like:



      public function product() {
      return $this->hasOne('AppProduct', 'key_id', 'key_id')
      ->join('products', 'products.key_id', 'orders.key_id')
      }


      How can i do this ?







      laravel-5 eloquent






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 16:42









      Wraith

      172114




      172114
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You have defined the relations between both the models, so If you fetch order with the following query:



          Order::where('order_id', 7)->with('product')->get();


          Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')



          After doing this, dump and die the $order, there will be an key pair with product and its values.






          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%2f53212316%2fhow-can-i-make-inner-join-in-method-instead-of-controller%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You have defined the relations between both the models, so If you fetch order with the following query:



            Order::where('order_id', 7)->with('product')->get();


            Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')



            After doing this, dump and die the $order, there will be an key pair with product and its values.






            share|improve this answer

























              up vote
              0
              down vote













              You have defined the relations between both the models, so If you fetch order with the following query:



              Order::where('order_id', 7)->with('product')->get();


              Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')



              After doing this, dump and die the $order, there will be an key pair with product and its values.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You have defined the relations between both the models, so If you fetch order with the following query:



                Order::where('order_id', 7)->with('product')->get();


                Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')



                After doing this, dump and die the $order, there will be an key pair with product and its values.






                share|improve this answer












                You have defined the relations between both the models, so If you fetch order with the following query:



                Order::where('order_id', 7)->with('product')->get();


                Remove the join line from product function i.e. ->join('products', 'products.key_id', 'orders.key_id')



                After doing this, dump and die the $order, there will be an key pair with product and its values.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 16:51









                engrhussainahmad

                1107




                1107






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212316%2fhow-can-i-make-inner-join-in-method-instead-of-controller%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

                    Landwehr

                    Reims

                    Schenkenzell