hasOne or BelongsTo in Laravel?












0















I have two tables: Photos and ProductPhotos where ProductPhotos is:



ProductPhotos
_______________
product_id | photo_id


And Photos is:



Photos
_______________
id | photo


Does it mean that relation beetween ProductPhotos -> Photos as belongsTo or hasOne yet?



Cause photo_id is foreign key, I guess it is belogsTo










share|improve this question



























    0















    I have two tables: Photos and ProductPhotos where ProductPhotos is:



    ProductPhotos
    _______________
    product_id | photo_id


    And Photos is:



    Photos
    _______________
    id | photo


    Does it mean that relation beetween ProductPhotos -> Photos as belongsTo or hasOne yet?



    Cause photo_id is foreign key, I guess it is belogsTo










    share|improve this question

























      0












      0








      0


      1






      I have two tables: Photos and ProductPhotos where ProductPhotos is:



      ProductPhotos
      _______________
      product_id | photo_id


      And Photos is:



      Photos
      _______________
      id | photo


      Does it mean that relation beetween ProductPhotos -> Photos as belongsTo or hasOne yet?



      Cause photo_id is foreign key, I guess it is belogsTo










      share|improve this question














      I have two tables: Photos and ProductPhotos where ProductPhotos is:



      ProductPhotos
      _______________
      product_id | photo_id


      And Photos is:



      Photos
      _______________
      id | photo


      Does it mean that relation beetween ProductPhotos -> Photos as belongsTo or hasOne yet?



      Cause photo_id is foreign key, I guess it is belogsTo







      laravel laravel-5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 23:19









      OPVOPV

      1,71921542




      1,71921542
























          3 Answers
          3






          active

          oldest

          votes


















          2














          A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.



          In your case, relations can be




          1. ProductPhotos belongsTo Products

          2. Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model


          You can have detail understanding on relationship on laravel Docs Relationships






          share|improve this answer































            1














            I would set up the relationships with
            photos->hasOne->ProductPhotos->belongsTo->Products
            Products->hasMany->ProductPhotos->belongsTo->Photos



            This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship



            https://laravel.com/docs/5.5/eloquent-relationships#has-many-through



            This will also depend on your use of the images of the products



            Hope this helps






            share|improve this answer































              -2














              In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.



              Use in both tables:



              belongsToMany


              https://laravel.com/docs/5.7/eloquent-relationships#many-to-many






              share|improve this answer
























              • I dont think so, cause I have intermediate table ProductPhoto that contains all photos links

                – OPV
                Nov 15 '18 at 23:38











              • So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table

                – Tácio Brito
                Nov 16 '18 at 12:28











              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%2f53329232%2fhasone-or-belongsto-in-laravel%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.



              In your case, relations can be




              1. ProductPhotos belongsTo Products

              2. Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model


              You can have detail understanding on relationship on laravel Docs Relationships






              share|improve this answer




























                2














                A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.



                In your case, relations can be




                1. ProductPhotos belongsTo Products

                2. Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model


                You can have detail understanding on relationship on laravel Docs Relationships






                share|improve this answer


























                  2












                  2








                  2







                  A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.



                  In your case, relations can be




                  1. ProductPhotos belongsTo Products

                  2. Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model


                  You can have detail understanding on relationship on laravel Docs Relationships






                  share|improve this answer













                  A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.



                  In your case, relations can be




                  1. ProductPhotos belongsTo Products

                  2. Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model


                  You can have detail understanding on relationship on laravel Docs Relationships







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 4:35









                  InvincibleElfInvincibleElf

                  9615




                  9615

























                      1














                      I would set up the relationships with
                      photos->hasOne->ProductPhotos->belongsTo->Products
                      Products->hasMany->ProductPhotos->belongsTo->Photos



                      This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship



                      https://laravel.com/docs/5.5/eloquent-relationships#has-many-through



                      This will also depend on your use of the images of the products



                      Hope this helps






                      share|improve this answer




























                        1














                        I would set up the relationships with
                        photos->hasOne->ProductPhotos->belongsTo->Products
                        Products->hasMany->ProductPhotos->belongsTo->Photos



                        This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship



                        https://laravel.com/docs/5.5/eloquent-relationships#has-many-through



                        This will also depend on your use of the images of the products



                        Hope this helps






                        share|improve this answer


























                          1












                          1








                          1







                          I would set up the relationships with
                          photos->hasOne->ProductPhotos->belongsTo->Products
                          Products->hasMany->ProductPhotos->belongsTo->Photos



                          This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship



                          https://laravel.com/docs/5.5/eloquent-relationships#has-many-through



                          This will also depend on your use of the images of the products



                          Hope this helps






                          share|improve this answer













                          I would set up the relationships with
                          photos->hasOne->ProductPhotos->belongsTo->Products
                          Products->hasMany->ProductPhotos->belongsTo->Photos



                          This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship



                          https://laravel.com/docs/5.5/eloquent-relationships#has-many-through



                          This will also depend on your use of the images of the products



                          Hope this helps







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 23:34









                          JoshJosh

                          718317




                          718317























                              -2














                              In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.



                              Use in both tables:



                              belongsToMany


                              https://laravel.com/docs/5.7/eloquent-relationships#many-to-many






                              share|improve this answer
























                              • I dont think so, cause I have intermediate table ProductPhoto that contains all photos links

                                – OPV
                                Nov 15 '18 at 23:38











                              • So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table

                                – Tácio Brito
                                Nov 16 '18 at 12:28
















                              -2














                              In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.



                              Use in both tables:



                              belongsToMany


                              https://laravel.com/docs/5.7/eloquent-relationships#many-to-many






                              share|improve this answer
























                              • I dont think so, cause I have intermediate table ProductPhoto that contains all photos links

                                – OPV
                                Nov 15 '18 at 23:38











                              • So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table

                                – Tácio Brito
                                Nov 16 '18 at 12:28














                              -2












                              -2








                              -2







                              In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.



                              Use in both tables:



                              belongsToMany


                              https://laravel.com/docs/5.7/eloquent-relationships#many-to-many






                              share|improve this answer













                              In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.



                              Use in both tables:



                              belongsToMany


                              https://laravel.com/docs/5.7/eloquent-relationships#many-to-many







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 15 '18 at 23:33









                              Tácio BritoTácio Brito

                              11




                              11













                              • I dont think so, cause I have intermediate table ProductPhoto that contains all photos links

                                – OPV
                                Nov 15 '18 at 23:38











                              • So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table

                                – Tácio Brito
                                Nov 16 '18 at 12:28



















                              • I dont think so, cause I have intermediate table ProductPhoto that contains all photos links

                                – OPV
                                Nov 15 '18 at 23:38











                              • So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table

                                – Tácio Brito
                                Nov 16 '18 at 12:28

















                              I dont think so, cause I have intermediate table ProductPhoto that contains all photos links

                              – OPV
                              Nov 15 '18 at 23:38





                              I dont think so, cause I have intermediate table ProductPhoto that contains all photos links

                              – OPV
                              Nov 15 '18 at 23:38













                              So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table

                              – Tácio Brito
                              Nov 16 '18 at 12:28





                              So in table ProductPhoto that contains all photos links, that you use belongsToMany, as in table Photos, where you reference the ProductPhotos table

                              – Tácio Brito
                              Nov 16 '18 at 12:28


















                              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%2f53329232%2fhasone-or-belongsto-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

                              Bressuire

                              Vorschmack

                              Quarantine