Eloquent eager load constrain by parent column values not working












0















I have this following tables;



shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


and below is the Models;



class Shipment extends Model {

protected $table = 'shipment';

public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}

class Item extends Model {

protected $table = 'item';
}


I tried to get the data for API;



return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();


but I am getting empty items. But weirdly I am getting the items data when I am using this; Shipment::find($id)->items. What did I miss?










share|improve this question























  • It's not possible to use $this in a relationship in combination with eager loading.

    – Jonas Staudenmeir
    Nov 22 '18 at 14:43











  • @JonasStaudenmeir Any workaround? I am trying to display the items, but I don't want to update the table structure because it's already correct to me.

    – sulaiman sudirman
    Nov 22 '18 at 15:41











  • Take a look at this package: github.com/topclaudy/compoships

    – Jonas Staudenmeir
    Nov 22 '18 at 15:43











  • @JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.

    – sulaiman sudirman
    Nov 22 '18 at 16:56
















0















I have this following tables;



shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


and below is the Models;



class Shipment extends Model {

protected $table = 'shipment';

public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}

class Item extends Model {

protected $table = 'item';
}


I tried to get the data for API;



return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();


but I am getting empty items. But weirdly I am getting the items data when I am using this; Shipment::find($id)->items. What did I miss?










share|improve this question























  • It's not possible to use $this in a relationship in combination with eager loading.

    – Jonas Staudenmeir
    Nov 22 '18 at 14:43











  • @JonasStaudenmeir Any workaround? I am trying to display the items, but I don't want to update the table structure because it's already correct to me.

    – sulaiman sudirman
    Nov 22 '18 at 15:41











  • Take a look at this package: github.com/topclaudy/compoships

    – Jonas Staudenmeir
    Nov 22 '18 at 15:43











  • @JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.

    – sulaiman sudirman
    Nov 22 '18 at 16:56














0












0








0








I have this following tables;



shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


and below is the Models;



class Shipment extends Model {

protected $table = 'shipment';

public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}

class Item extends Model {

protected $table = 'item';
}


I tried to get the data for API;



return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();


but I am getting empty items. But weirdly I am getting the items data when I am using this; Shipment::find($id)->items. What did I miss?










share|improve this question














I have this following tables;



shipment
==========
date document_no address_id
----------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


item
=======
shipment_date document_no address_id
-------------------------------------
2018-11-20 SO-18-11971 0
2018-11-20 SO-18-11971 1
2018-11-21 SO-18-11972 0


and below is the Models;



class Shipment extends Model {

protected $table = 'shipment';

public function items()
{
return $this->hasMany(Item::class, 'document_no', 'document_no')
->where(['shipment_date' => $this->date, 'address_id' => $this->address_id]);
}
}

class Item extends Model {

protected $table = 'item';
}


I tried to get the data for API;



return Shipment::with('items')->where('date', $shipment_date)->get()->toArray();


but I am getting empty items. But weirdly I am getting the items data when I am using this; Shipment::find($id)->items. What did I miss?







laravel laravel-5 eloquent






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 13:15









sulaiman sudirmansulaiman sudirman

1,2111422




1,2111422













  • It's not possible to use $this in a relationship in combination with eager loading.

    – Jonas Staudenmeir
    Nov 22 '18 at 14:43











  • @JonasStaudenmeir Any workaround? I am trying to display the items, but I don't want to update the table structure because it's already correct to me.

    – sulaiman sudirman
    Nov 22 '18 at 15:41











  • Take a look at this package: github.com/topclaudy/compoships

    – Jonas Staudenmeir
    Nov 22 '18 at 15:43











  • @JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.

    – sulaiman sudirman
    Nov 22 '18 at 16:56



















  • It's not possible to use $this in a relationship in combination with eager loading.

    – Jonas Staudenmeir
    Nov 22 '18 at 14:43











  • @JonasStaudenmeir Any workaround? I am trying to display the items, but I don't want to update the table structure because it's already correct to me.

    – sulaiman sudirman
    Nov 22 '18 at 15:41











  • Take a look at this package: github.com/topclaudy/compoships

    – Jonas Staudenmeir
    Nov 22 '18 at 15:43











  • @JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.

    – sulaiman sudirman
    Nov 22 '18 at 16:56

















It's not possible to use $this in a relationship in combination with eager loading.

– Jonas Staudenmeir
Nov 22 '18 at 14:43





It's not possible to use $this in a relationship in combination with eager loading.

– Jonas Staudenmeir
Nov 22 '18 at 14:43













@JonasStaudenmeir Any workaround? I am trying to display the items, but I don't want to update the table structure because it's already correct to me.

– sulaiman sudirman
Nov 22 '18 at 15:41





@JonasStaudenmeir Any workaround? I am trying to display the items, but I don't want to update the table structure because it's already correct to me.

– sulaiman sudirman
Nov 22 '18 at 15:41













Take a look at this package: github.com/topclaudy/compoships

– Jonas Staudenmeir
Nov 22 '18 at 15:43





Take a look at this package: github.com/topclaudy/compoships

– Jonas Staudenmeir
Nov 22 '18 at 15:43













@JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.

– sulaiman sudirman
Nov 22 '18 at 16:56





@JonasStaudenmeir thanks, please post this as answer, i'll mark it as correct answer. Although unfortunately this only solved half of my problem because of my use case is more complicated, but i'll create a separate question.

– sulaiman sudirman
Nov 22 '18 at 16:56












2 Answers
2






active

oldest

votes


















0














It's not possible to use $this in a relationship in combination with eager loading.



Take a look at this package: https://github.com/topclaudy/compoships






share|improve this answer































    0














    that relation wont work correctly cause the structure is incorrect. try it like this



    shipments
    ==========
    id date document_no address_id
    ----------------------------
    1 2018-11-20 SO-18-11971 0
    2 2018-11-20 SO-18-11971 1
    3 2018-11-21 SO-18-11972 0


    item
    =======
    shipment_id
    -------------------------------------
    1
    2
    3


    Less redundancy and more efficient. the relation will be



    class Shipment extends Model {

    public function items()
    {
    return $this->hasMany(Item::class);
    }
    }


    take in count in my example i changed the table name from shipment to shipments so you dont need to declare the table or the foreign key.



    that said, Eloquent (and Laravel) doesn't support multi key index in relations.






    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%2f53431848%2feloquent-eager-load-constrain-by-parent-column-values-not-working%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









      0














      It's not possible to use $this in a relationship in combination with eager loading.



      Take a look at this package: https://github.com/topclaudy/compoships






      share|improve this answer




























        0














        It's not possible to use $this in a relationship in combination with eager loading.



        Take a look at this package: https://github.com/topclaudy/compoships






        share|improve this answer


























          0












          0








          0







          It's not possible to use $this in a relationship in combination with eager loading.



          Take a look at this package: https://github.com/topclaudy/compoships






          share|improve this answer













          It's not possible to use $this in a relationship in combination with eager loading.



          Take a look at this package: https://github.com/topclaudy/compoships







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 17:22









          Jonas StaudenmeirJonas Staudenmeir

          12.9k2934




          12.9k2934

























              0














              that relation wont work correctly cause the structure is incorrect. try it like this



              shipments
              ==========
              id date document_no address_id
              ----------------------------
              1 2018-11-20 SO-18-11971 0
              2 2018-11-20 SO-18-11971 1
              3 2018-11-21 SO-18-11972 0


              item
              =======
              shipment_id
              -------------------------------------
              1
              2
              3


              Less redundancy and more efficient. the relation will be



              class Shipment extends Model {

              public function items()
              {
              return $this->hasMany(Item::class);
              }
              }


              take in count in my example i changed the table name from shipment to shipments so you dont need to declare the table or the foreign key.



              that said, Eloquent (and Laravel) doesn't support multi key index in relations.






              share|improve this answer




























                0














                that relation wont work correctly cause the structure is incorrect. try it like this



                shipments
                ==========
                id date document_no address_id
                ----------------------------
                1 2018-11-20 SO-18-11971 0
                2 2018-11-20 SO-18-11971 1
                3 2018-11-21 SO-18-11972 0


                item
                =======
                shipment_id
                -------------------------------------
                1
                2
                3


                Less redundancy and more efficient. the relation will be



                class Shipment extends Model {

                public function items()
                {
                return $this->hasMany(Item::class);
                }
                }


                take in count in my example i changed the table name from shipment to shipments so you dont need to declare the table or the foreign key.



                that said, Eloquent (and Laravel) doesn't support multi key index in relations.






                share|improve this answer


























                  0












                  0








                  0







                  that relation wont work correctly cause the structure is incorrect. try it like this



                  shipments
                  ==========
                  id date document_no address_id
                  ----------------------------
                  1 2018-11-20 SO-18-11971 0
                  2 2018-11-20 SO-18-11971 1
                  3 2018-11-21 SO-18-11972 0


                  item
                  =======
                  shipment_id
                  -------------------------------------
                  1
                  2
                  3


                  Less redundancy and more efficient. the relation will be



                  class Shipment extends Model {

                  public function items()
                  {
                  return $this->hasMany(Item::class);
                  }
                  }


                  take in count in my example i changed the table name from shipment to shipments so you dont need to declare the table or the foreign key.



                  that said, Eloquent (and Laravel) doesn't support multi key index in relations.






                  share|improve this answer













                  that relation wont work correctly cause the structure is incorrect. try it like this



                  shipments
                  ==========
                  id date document_no address_id
                  ----------------------------
                  1 2018-11-20 SO-18-11971 0
                  2 2018-11-20 SO-18-11971 1
                  3 2018-11-21 SO-18-11972 0


                  item
                  =======
                  shipment_id
                  -------------------------------------
                  1
                  2
                  3


                  Less redundancy and more efficient. the relation will be



                  class Shipment extends Model {

                  public function items()
                  {
                  return $this->hasMany(Item::class);
                  }
                  }


                  take in count in my example i changed the table name from shipment to shipments so you dont need to declare the table or the foreign key.



                  that said, Eloquent (and Laravel) doesn't support multi key index in relations.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 13:24









                  N69SN69S

                  1,125514




                  1,125514






























                      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%2f53431848%2feloquent-eager-load-constrain-by-parent-column-values-not-working%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

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith