switchon@lavalamp.biz
+27(0) 83 419 4851 / +27(0) 21 036 1165
Application & software development
Get A Quote

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact

    • Home
    • Services
      • Application & software development
      • Outsourced software development
      • Project based resourcing
      • Digital marketing & consulting
      • Graphic design & consulting
      • UI / UX design & consulting
      • Recruitment services
      • Lease an expert
    • About
      • How we work
      • NBConsult Group
      • Partners
      • Lightbox Digital
    • Blog
    • Join us
    • Contact
    back pain
    How To Sit For 8+ Hours A Day And Not Destroy Your Body
    Nov 23, 2020
    9 tips to prevent programmer burnout
    9 Tips To Prevent Programmer Burnout
    Jan 18, 2021

    Laravel Pivot Tables

    Categories
    • Software Development
    Tags
    • database
    • laravel
    • models
    • pivot tables
    laravel pivot tables
    laravel pivot tables

    I've recently run into the following scenario; A Store needed to be linked to multiple Products. I could have added a store_id to the Products table, but then what if I wanted to use the Product elsewhere?

    The solution was a Pivot table, linking a Store to a Product in a separate table.



    Database


    Starting off with the database structure, we'll have to create both the Store and Product table, as well as the Pivot table. See below:

    stores:

    • id
    • name

    products

    • id
    • name

    product_store:

    • id
    • product_id
    • store_id

    With "product_store" being the Pivot table.


    Models


    The Models need relationships to link the Store to the Product. This is a Many-to-many relationship, so our Models will reflect that. See below:

    App\Store:

    laravel pivot tables

    App\Product:

    laravel pivot tables

    You have to define both the relationship and the inverse. Now that we have finished that, we can make use of the relationship.



    Adding Products to Stores


    To start, we save data the same way we normally do. The only difference now is that we have to manually attach the product to the store. See below.

    laravel pivot tables

    Contact us


      Related posts:

      laravel livewireLaravel Livewire simple authentication with guards in laravelSimple Authentication With Guards In Laravel how to setup git-ftp for windowsHow To Setup Git-ftp For Windows 9 tips to prevent programmer burnout9 Tips To Prevent Programmer Burnout
      Share
      79
      Taylor Plaatjies
      Taylor Plaatjies

      Leave a Reply Cancel reply

      Your email address will not be published. Required fields are marked *

      Lava Lamp Lab


      Like technology, a lava lamp constantly changes form, producing new conditions with every passing moment



      lava lamp lab facebook   lava lamp lab twitter   lava lamp lab linkedin   lava lamp lab instgram

      Services


      Application & software development

      Outsourced software development

      Project based resourcing

      Digital marketing & consulting

      Graphic design & consulting

      UI / UX design & consulting

      Contact Us


      +27(0) 83 419 4851

      +27(0) 21 036 1165


      switchon@lavalamp.biz


      Unit 4 Monaco Square,
      14 Church Street,
      Durbanville,
      Cape Town, 7550

      NBConsult Group


      nbconsult
      nbconnect msp
      nbclearning
      river broadband
      designer needed
      © 2023 Lava Lamp Lab (Pty) Ltd | All Rights Reserved | Privacy Policy
      Contact us now

        Application & software development

          Outsourced software development

            Project based resourcing

              Digital marketing & consulting

                Graphic design & consulting

                  UI/UX design & consulting

                    Lease an expert

                      Recruitment services

                        We are using cookies to give you the best experience on our website.

                        You can find out more about which cookies we are using or switch them off in settings.

                        Lava Lamp Lab
                        Powered by  GDPR Cookie Compliance
                        Privacy Overview

                        This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

                        Strictly Necessary Cookies

                        Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

                        If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

                        3rd Party Cookies

                        This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

                        Keeping this cookie enabled helps us to improve our website.

                        Please enable Strictly Necessary Cookies first so that we can save your preferences!

                        Cookie Policy

                        More information about our Cookie Policy