Eloquent queries work separately but not together in a subquery2019 Community Moderator ElectionHow does PHP 'foreach' actually work?How to create a subquery using Laravel Eloquent?Laravel's Eloquent ORM versus Query BuilderWhy do Eloquent has and whereHas methods use subqueries instead of joins?Laravel Eloquent - distinct() and count() not working properly togetherLaravel. Eloquent queryGet Laravel Eloquent Collection from raw queryhow to write >= eloquent database query in laravel 5.2Merge eloquent collection with query in Laravel?SQL query to Eloquent

Do I really need to have a scientific explanation for my premise?

Do items de-spawn in Diablo?

Rewrite the power sum in terms of convolution

How did Alan Turing break the enigma code using the hint given by the lady in the bar?

Makefile strange variable substitution

They call me Inspector Morse

List elements digit difference sort

Does the nature of the Apocalypse in The Umbrella Academy change from the first to the last episode?

What's the "normal" opposite of flautando?

How strictly should I take "Candidates must be local"?

Is "conspicuously missing" or "conspicuously" the subject of this sentence?

Why the color red for the Republican Party

meaning and function of 幸 in "则幸分我一杯羹"

Bash script should only kill those instances of another script's that it has launched

Reversed Sudoku

Was Luke Skywalker the leader of the Rebel forces on Hoth?

How can I ensure my trip to the UK will not have to be cancelled because of Brexit?

How to detect if C code (which needs 'extern C') is compiled in C++

Word for a person who has no opinion about whether god exists

Declaring and defining template, and specialising them

Doesn't allowing a user mode program to access kernel space memory and execute the IN and OUT instructions defeat the purpose of having CPU modes?

How can I get players to stop ignoring or overlooking the plot hooks I'm giving them?

What problems would a superhuman have whose skin is constantly hot?

How to secure an aircraft at a transient parking space?



Eloquent queries work separately but not together in a subquery



2019 Community Moderator ElectionHow does PHP 'foreach' actually work?How to create a subquery using Laravel Eloquent?Laravel's Eloquent ORM versus Query BuilderWhy do Eloquent has and whereHas methods use subqueries instead of joins?Laravel Eloquent - distinct() and count() not working properly togetherLaravel. Eloquent queryGet Laravel Eloquent Collection from raw queryhow to write >= eloquent database query in laravel 5.2Merge eloquent collection with query in Laravel?SQL query to Eloquent










2















I'm trying to fetch some data with a subquery using Eloquent but dding returns nothing. Separately, this



 $discountArticles = $discountTableItemIdIn
->where('recipient_type', '=', 'article')
->toArray();


or this



 $discountArticles = $discountTableItemIdIn
->where('recipient_id', '=', $articleId)
->toArray();


work fine.



However when I try something like this, it fails (or rather, returns nothing):



 $discountArticles = $discountTableItemIdIn->where(function ($subQuery) 
$subQuery
->where('recipient_type', '=', 'article')
->where('recipient_id', '=', $articleId);
)->toArray();


I know I can do separate queries on the same collection and do an array_merge but I'd like to get this way working instead. Not sure what's happening.










share|improve this question




























    2















    I'm trying to fetch some data with a subquery using Eloquent but dding returns nothing. Separately, this



     $discountArticles = $discountTableItemIdIn
    ->where('recipient_type', '=', 'article')
    ->toArray();


    or this



     $discountArticles = $discountTableItemIdIn
    ->where('recipient_id', '=', $articleId)
    ->toArray();


    work fine.



    However when I try something like this, it fails (or rather, returns nothing):



     $discountArticles = $discountTableItemIdIn->where(function ($subQuery) 
    $subQuery
    ->where('recipient_type', '=', 'article')
    ->where('recipient_id', '=', $articleId);
    )->toArray();


    I know I can do separate queries on the same collection and do an array_merge but I'd like to get this way working instead. Not sure what's happening.










    share|improve this question


























      2












      2








      2








      I'm trying to fetch some data with a subquery using Eloquent but dding returns nothing. Separately, this



       $discountArticles = $discountTableItemIdIn
      ->where('recipient_type', '=', 'article')
      ->toArray();


      or this



       $discountArticles = $discountTableItemIdIn
      ->where('recipient_id', '=', $articleId)
      ->toArray();


      work fine.



      However when I try something like this, it fails (or rather, returns nothing):



       $discountArticles = $discountTableItemIdIn->where(function ($subQuery) 
      $subQuery
      ->where('recipient_type', '=', 'article')
      ->where('recipient_id', '=', $articleId);
      )->toArray();


      I know I can do separate queries on the same collection and do an array_merge but I'd like to get this way working instead. Not sure what's happening.










      share|improve this question
















      I'm trying to fetch some data with a subquery using Eloquent but dding returns nothing. Separately, this



       $discountArticles = $discountTableItemIdIn
      ->where('recipient_type', '=', 'article')
      ->toArray();


      or this



       $discountArticles = $discountTableItemIdIn
      ->where('recipient_id', '=', $articleId)
      ->toArray();


      work fine.



      However when I try something like this, it fails (or rather, returns nothing):



       $discountArticles = $discountTableItemIdIn->where(function ($subQuery) 
      $subQuery
      ->where('recipient_type', '=', 'article')
      ->where('recipient_id', '=', $articleId);
      )->toArray();


      I know I can do separate queries on the same collection and do an array_merge but I'd like to get this way working instead. Not sure what's happening.







      php eloquent






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 6 at 15:40







      Mike K

















      asked Mar 6 at 15:26









      Mike KMike K

      1981211




      1981211






















          1 Answer
          1






          active

          oldest

          votes


















          1














          So $discountTableItemIdIn is a collection of the entire table? That means you're gonna need a different function, as the ->where() logic on a collection is different from how it functions on a builder (eloquent) instance.



          Try using filter():



          $discountArticles = $discountTableItemIdIn->filter(function ($item) use($articleId) 
          return $item->recipient_type == "article" && $item->recipient_id == $articleId;
          )->toArray();


          What this will do is filter your $discountTableItemIdIn collection for records that have a type of article and a recipient_id of whatever $articleId contains, return a new collection and convert that to an array.



          Just a note, this is quite inefficient; you should try to avoid loading the whole table into a collection and just query the table directly using the subquery logic in your question.






          share|improve this answer























          • Thanks! I will try to see if this work. As for your note, the problem is that there are many foreach iterations in my cron, and each queries different tables (thousands of queries) with unique IDs of different objects. The cron took 8 hours originally.. this way it'll kill the server with inefficiency, but at least it should be faster. Perhaps you have another suggestion to this approach? Im fairly new to Eloquent and PHP in general.

            – Mike K
            Mar 6 at 15:54











          • Ah; I see, that makes sense. From experience, I've found that leveraging the heavy lifting to SQL is always performant-plus over having Collections handle it, but I can't say I've done anything to the scale you're describing. 8 hours is an ungodly long time for a script to run, and if this approach performs better than that, then it should be ok. Just be aware of the differences between Eloquent and Collection methods and you should be fine.

            – Tim Lewis
            Mar 6 at 15:57






          • 1





            Thank you. Your method worked btw, (or at least, it's a great starting point!). Cheers

            – Mike K
            Mar 6 at 15:59










          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%2f55026623%2feloquent-queries-work-separately-but-not-together-in-a-subquery%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









          1














          So $discountTableItemIdIn is a collection of the entire table? That means you're gonna need a different function, as the ->where() logic on a collection is different from how it functions on a builder (eloquent) instance.



          Try using filter():



          $discountArticles = $discountTableItemIdIn->filter(function ($item) use($articleId) 
          return $item->recipient_type == "article" && $item->recipient_id == $articleId;
          )->toArray();


          What this will do is filter your $discountTableItemIdIn collection for records that have a type of article and a recipient_id of whatever $articleId contains, return a new collection and convert that to an array.



          Just a note, this is quite inefficient; you should try to avoid loading the whole table into a collection and just query the table directly using the subquery logic in your question.






          share|improve this answer























          • Thanks! I will try to see if this work. As for your note, the problem is that there are many foreach iterations in my cron, and each queries different tables (thousands of queries) with unique IDs of different objects. The cron took 8 hours originally.. this way it'll kill the server with inefficiency, but at least it should be faster. Perhaps you have another suggestion to this approach? Im fairly new to Eloquent and PHP in general.

            – Mike K
            Mar 6 at 15:54











          • Ah; I see, that makes sense. From experience, I've found that leveraging the heavy lifting to SQL is always performant-plus over having Collections handle it, but I can't say I've done anything to the scale you're describing. 8 hours is an ungodly long time for a script to run, and if this approach performs better than that, then it should be ok. Just be aware of the differences between Eloquent and Collection methods and you should be fine.

            – Tim Lewis
            Mar 6 at 15:57






          • 1





            Thank you. Your method worked btw, (or at least, it's a great starting point!). Cheers

            – Mike K
            Mar 6 at 15:59















          1














          So $discountTableItemIdIn is a collection of the entire table? That means you're gonna need a different function, as the ->where() logic on a collection is different from how it functions on a builder (eloquent) instance.



          Try using filter():



          $discountArticles = $discountTableItemIdIn->filter(function ($item) use($articleId) 
          return $item->recipient_type == "article" && $item->recipient_id == $articleId;
          )->toArray();


          What this will do is filter your $discountTableItemIdIn collection for records that have a type of article and a recipient_id of whatever $articleId contains, return a new collection and convert that to an array.



          Just a note, this is quite inefficient; you should try to avoid loading the whole table into a collection and just query the table directly using the subquery logic in your question.






          share|improve this answer























          • Thanks! I will try to see if this work. As for your note, the problem is that there are many foreach iterations in my cron, and each queries different tables (thousands of queries) with unique IDs of different objects. The cron took 8 hours originally.. this way it'll kill the server with inefficiency, but at least it should be faster. Perhaps you have another suggestion to this approach? Im fairly new to Eloquent and PHP in general.

            – Mike K
            Mar 6 at 15:54











          • Ah; I see, that makes sense. From experience, I've found that leveraging the heavy lifting to SQL is always performant-plus over having Collections handle it, but I can't say I've done anything to the scale you're describing. 8 hours is an ungodly long time for a script to run, and if this approach performs better than that, then it should be ok. Just be aware of the differences between Eloquent and Collection methods and you should be fine.

            – Tim Lewis
            Mar 6 at 15:57






          • 1





            Thank you. Your method worked btw, (or at least, it's a great starting point!). Cheers

            – Mike K
            Mar 6 at 15:59













          1












          1








          1







          So $discountTableItemIdIn is a collection of the entire table? That means you're gonna need a different function, as the ->where() logic on a collection is different from how it functions on a builder (eloquent) instance.



          Try using filter():



          $discountArticles = $discountTableItemIdIn->filter(function ($item) use($articleId) 
          return $item->recipient_type == "article" && $item->recipient_id == $articleId;
          )->toArray();


          What this will do is filter your $discountTableItemIdIn collection for records that have a type of article and a recipient_id of whatever $articleId contains, return a new collection and convert that to an array.



          Just a note, this is quite inefficient; you should try to avoid loading the whole table into a collection and just query the table directly using the subquery logic in your question.






          share|improve this answer













          So $discountTableItemIdIn is a collection of the entire table? That means you're gonna need a different function, as the ->where() logic on a collection is different from how it functions on a builder (eloquent) instance.



          Try using filter():



          $discountArticles = $discountTableItemIdIn->filter(function ($item) use($articleId) 
          return $item->recipient_type == "article" && $item->recipient_id == $articleId;
          )->toArray();


          What this will do is filter your $discountTableItemIdIn collection for records that have a type of article and a recipient_id of whatever $articleId contains, return a new collection and convert that to an array.



          Just a note, this is quite inefficient; you should try to avoid loading the whole table into a collection and just query the table directly using the subquery logic in your question.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 6 at 15:50









          Tim LewisTim Lewis

          11k63664




          11k63664












          • Thanks! I will try to see if this work. As for your note, the problem is that there are many foreach iterations in my cron, and each queries different tables (thousands of queries) with unique IDs of different objects. The cron took 8 hours originally.. this way it'll kill the server with inefficiency, but at least it should be faster. Perhaps you have another suggestion to this approach? Im fairly new to Eloquent and PHP in general.

            – Mike K
            Mar 6 at 15:54











          • Ah; I see, that makes sense. From experience, I've found that leveraging the heavy lifting to SQL is always performant-plus over having Collections handle it, but I can't say I've done anything to the scale you're describing. 8 hours is an ungodly long time for a script to run, and if this approach performs better than that, then it should be ok. Just be aware of the differences between Eloquent and Collection methods and you should be fine.

            – Tim Lewis
            Mar 6 at 15:57






          • 1





            Thank you. Your method worked btw, (or at least, it's a great starting point!). Cheers

            – Mike K
            Mar 6 at 15:59

















          • Thanks! I will try to see if this work. As for your note, the problem is that there are many foreach iterations in my cron, and each queries different tables (thousands of queries) with unique IDs of different objects. The cron took 8 hours originally.. this way it'll kill the server with inefficiency, but at least it should be faster. Perhaps you have another suggestion to this approach? Im fairly new to Eloquent and PHP in general.

            – Mike K
            Mar 6 at 15:54











          • Ah; I see, that makes sense. From experience, I've found that leveraging the heavy lifting to SQL is always performant-plus over having Collections handle it, but I can't say I've done anything to the scale you're describing. 8 hours is an ungodly long time for a script to run, and if this approach performs better than that, then it should be ok. Just be aware of the differences between Eloquent and Collection methods and you should be fine.

            – Tim Lewis
            Mar 6 at 15:57






          • 1





            Thank you. Your method worked btw, (or at least, it's a great starting point!). Cheers

            – Mike K
            Mar 6 at 15:59
















          Thanks! I will try to see if this work. As for your note, the problem is that there are many foreach iterations in my cron, and each queries different tables (thousands of queries) with unique IDs of different objects. The cron took 8 hours originally.. this way it'll kill the server with inefficiency, but at least it should be faster. Perhaps you have another suggestion to this approach? Im fairly new to Eloquent and PHP in general.

          – Mike K
          Mar 6 at 15:54





          Thanks! I will try to see if this work. As for your note, the problem is that there are many foreach iterations in my cron, and each queries different tables (thousands of queries) with unique IDs of different objects. The cron took 8 hours originally.. this way it'll kill the server with inefficiency, but at least it should be faster. Perhaps you have another suggestion to this approach? Im fairly new to Eloquent and PHP in general.

          – Mike K
          Mar 6 at 15:54













          Ah; I see, that makes sense. From experience, I've found that leveraging the heavy lifting to SQL is always performant-plus over having Collections handle it, but I can't say I've done anything to the scale you're describing. 8 hours is an ungodly long time for a script to run, and if this approach performs better than that, then it should be ok. Just be aware of the differences between Eloquent and Collection methods and you should be fine.

          – Tim Lewis
          Mar 6 at 15:57





          Ah; I see, that makes sense. From experience, I've found that leveraging the heavy lifting to SQL is always performant-plus over having Collections handle it, but I can't say I've done anything to the scale you're describing. 8 hours is an ungodly long time for a script to run, and if this approach performs better than that, then it should be ok. Just be aware of the differences between Eloquent and Collection methods and you should be fine.

          – Tim Lewis
          Mar 6 at 15:57




          1




          1





          Thank you. Your method worked btw, (or at least, it's a great starting point!). Cheers

          – Mike K
          Mar 6 at 15:59





          Thank you. Your method worked btw, (or at least, it's a great starting point!). Cheers

          – Mike K
          Mar 6 at 15:59



















          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%2f55026623%2feloquent-queries-work-separately-but-not-together-in-a-subquery%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

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко