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
    how we made a build server using webhooks, fastlane and bash (part 1)
    How We Made A Build Server Using Webhooks, Fastlane And Bash (Part 2)
    Jan 27, 2019
    kotlin
    Android: A Quick Kotlin Eksperience
    Apr 4, 2019

    Local Scopes In Laravel

    laravel logo
    laravel logo

    Recently, while working on a project for a client I needed to be able to filter out some results based on a value. After searching the internet for a while, I came across Scopes in the Laravel documentation.  I decided to use Local Scopes, instead of Global Scopes, because I don’t want every call to the database to have the scope applied.


    Let’s set the scene:


    Imagine you are designing a program for a gym that displays a list of Activities a user can sign up for. These Activities can have one or more Schedules. These Schedules should be displayed under an Activity, but only when they are published. I’m using published in my case, but active/show/etc can be used too.

    The Activity Model has:

    id
    name
    description
    

    and the Schedule Model has:

    activity_id
    start_time
    end_time
    price
    address
    published
    

    Right now, if we defined a relationship between Activities and Schedules

    // App\Schedule.php
    public function activity(){
        return $this->belongsTo(Activity::class);
    }
    // App\Activity.php
    public function schedule(){
        return $this->hasMany(Schedule::class);
    }
    

    and tried to get the Activities with return Activity::with(‘schedules’)->get(); we would get a result that looks like this:

    {  
       "data":{  
          "activity":{  
             "id":1,
             "name":"Yoga in the Woods",
             "description":"Join us this Friday as we learn the basic Yoga poses!",
             "schedules":[  
                {  
                   "id":1,
                   "activity_id":1,
                   "start_time":"12:00",
                   "end_time":"13:00",
                   "address":"12 Fake Street, Some Suburb",
                   "published":false
                },
                {  
                   "id":2,
                   "activity_id":1,
                   "start_time":"13:00",
                   "end_time":"15:00",
                   "address":"12 Fake Street, Some Suburb",
                   "published":true
                }
             ]
          }
       }
    }
    

    Notice that on the second schedules objects, the published parameter is false. We don’t want someone seeing a schedule when it’s not published for obvious reasons.

    So let’s write a Scope!

    What are scopes? Taken shamelessly from the Laravel Docs

    Scopes allow you to define common sets of constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all users that are considered "popular". To define a scope, prefix an Eloquent model method with scope.Scopes should always return a query builder instance.

    Defining a scope is straightforward. Inside of the Schedule Model we can define one like so:

    /**
      * Scope a query to only include published schedules. 
      * 
      * @param \Illuminate\Database\Eloquent\Builder $query 
      * @return \Illuminate\Database\Eloquent\Builder 
      */ 
    public function scopePublished($query) {
         return $query->where('published', true);
    }
    

    And that’s your scope, pretty simple stuff.In order to utilise the scope all you need to do is apply is to your query. Just remember to not include the scope prefix when doing so:

    return Schedule::published()->get();

    and you should get a result similar to this:

    {  
       "schedules":[  
          {  
             "id":2,
             "activity_id":1,
             "start_time":"13:00",
             "end_time":"15:00",
             "address":"12 Fake Street, Some Suburb",
             "published":true
          }
       ]
    }
    

    Notice that this time we only have the schedule that has been published. Now in some cases, you might be happy, but in mine I needed a little more flexibility. I didn’t want any activity to show. I wanted only activities with published schedules to appear on the list.

    So let’s define a method on our Activity model that allows us to use the scope in the Schedule model:

    public function publishedSchedule() { 
        return $this->hasMany(Schedule::class)->published(); 
        // or this way: 
        // return $this->schedules()->published(); 
    }
    

    What this method does is it defines a relationship between Activities and Schedules, but only where the published property is true. If we edit our query a bit we get only the activities with published schedules.

    $activity = Activity::whereHas('publishedSchedule')->get();

    We can go a little deeper and show the activities with published schedules.

    $activity = Activity::whereHas('schedulePublished')->with(['schedule' => function($q) { 
        $q->published();
    }])->get();

    Result:

    {  
       "data":{  
          "activity":{  
             "id":1,
             "name":"Yoga in the Woods",
             "description":"Join us this Friday as we learn the basic Yoga poses!",
             "schedules":[  
                {  
                   "id":2,
                   "activity_id":1,
                   "start_time":"13:00",
                   "end_time":"15:00",
                   "address":"12 Fake Street, Some Suburb",
                   "published":true
                }
             ]
          }
       }
    }
    

    And there you have it. Creating a utilising local scopes in a Laravel Project

    Contact us


      Related posts:

      dynamic doughnut graph using laravel vue componentDynamic Doughnut Graph Using Laravel Vue Component laravel livewireLaravel Livewire laravel observersLaravel Observers laravel envoyer and forgeProvisioning A New Digital Ocean Server With Laravel Forge, And Deploying Code With Laravel Envoyer
      Share
      25
      Gary Irwin
      Gary Irwin
      Gary is currently the CEO of Lava Lamp Lab, he has extensive experience in ICT Services & Business with global experience focusing on software and product development in emerging market. Working with High-calibre customers to deliver an array of B2B & Consumer Applications. Though Lava Lamp Lab, he leads multi-skilled teams in development & implementation of integrated IT solutions for companies through harnessing IoT (Internet of Things) , ML (Machine Learning) Platforms, Big Data / BI technologies. He consults on Application Modernization, Product Development, Ideation and Delivery Management of Solutions.

      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 1 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