|

Global Query Scopes And Pagination

Global Query Scopes And Pagination

In this tutorial, we’ll talk about Global Query Scopes and Pagination among other topics. First off, we’ll make some adjustments to the current view file that displays a single thread. Right now the content is centered in a one column style layout. We will modify this to a two column layout with the main content on the left hand side. We’ll then add a smaller right hand column, and then start adding some basic meta type information that might be helpful to the user of the application. Along the way we’ll tackle global query scopes, the str_plural helper function, and simple pagination in Laravel. Let’s get started.


Refactoring threads/show.blade.php

Let’s shift things around a bit on the front end to make room for some additional features on the page. Currently, when viewing a single thread with it’s replies, everything is lined up right in the center of the page. We can refactor this to move things to the left a bit, opening up room for meta type information on the right hand side. Go ahead and open up the threads/show.blade.php and we can make these adjustments.


Using Bootstrap Offset To Control Layout

First off, we’ll remove the offset on some columns like so:
bootstrap columsn offset example

This column is within a Bootstrap row. Removing the offset does move this element right on over to the left hand side like so:
bootstrap no offset column

Of course the title is now shifted to the left, but the replies are still in the middle. Let’s shift those over as well. For this, we will remove an entire row and re position the foreach loop in the view.
moving the foreach in the view

This has the result of pushing our replies to threads over to the left, just like our main thread.
replies are now moved to the left

Finally, we are going to need to move the reply form for threads to the left as well. Ok, that is easy enough. We just need to remove a row, and place the code for the form right after that foreach we just moved. We’ll just place the full code for this here.

Now we have everything lined up, and there is plenty of room on the right hand side of the page to add some other information about the thread or user.
bootstrap left side layout


Using Basic Math For Bootstrap Columns

We know that Bootstrap columns are broken up into 12 total units. On the left hand side of the page, we are taking up 8 units so far. Well, we have 4 left over for the total width. We can add a column with 4 unit spaces to take up the rest of the right hand side on the page. This will give us the 12 total width for the full page. The highlighted code below, gives us that new column on the right side of the page.

Looking pretty good!
bootstrap 8 column left 4 column right


Using Right Hand Column For Meta

In that right hand column, we can now place some information about the thread or user. For example we can use the Carbon Instance tied to our variable to display how long ago the thread was posted. That is really easy to do by using this code.

Nice!
carbon diffForHumans example


Introducing Global Query Scopes

A global query scope in Laravel is a query scope that is automatically applied to all of the queries. This is per model. So for example if we add a global query scope to the Thread model, we mean that for all of the Thread queries that get run, the application must also apply what is defined in the query scope. Let’s show some code to see how that works.

Open up the Thread.php model, and we will place the global query scope inside of the boot() method. Laravel is designed to trigger this method automatically.

Ok we see this, but what is the purpose? Well, shortly we will add some additional details such as the reply count to the right hand side column. It is possible to simply use our existing model relations to fetch this data. This approach is an alternative if you would like a way to just make those reply counts available as their own property and with a lesser number of SQL queries getting triggered on the database. Essentially, thanks to the code above, we now have a replies_count property we can reference in the view. Let’s see.

Looking at this now, we can see that it is indeed working.
global scope laravel example


How str_plural() works

We can now take a look at a really useful laravel helper function that makes intelligent decisions on how to pluralize or not pluralize a particular word, based on the number of that particular thing has. For example, if we have 3 replies to a thread, we want to see 3 replies on the page. If we only have 1 reply however, we don’t want the page to show 1 replies. How can we fix that? You can fix that with str_plural(). Currently, we have ‘replies’ hard coded into the view. We are going to remove the hard coded ‘replies’ and replace that with {{ str_plural(‘reply’, $thread->replies_count) }} like so.

The str_plural() helper function takes two arguments. The first argument is a string, which is the singular word you are dealing with. The second argument is an integer which represents the count of how many of those things you have. So with our code in place, if we visit a thread that has only 1 reply, it is correctly saying “1 reply” and not “1 replies”. Very nice.
str_plural example use case


Adding Pagination To Replies

We can add pagination to the replies section pretty easily. We can make one small adjustment to the show() method in the ThreadsController, and also update the markup in the threads/show.blade.php view to see this work. First, open up ThreadsController.php and adjust the code like so.

Controller

View

Pagination is complete
laravel pagination example


Global Query Scopes And Pagination Summary

This was a fun little tutorial that got our threads/show.blade.php view fixed up with some cool meta information. We also learned about how to set up global query scopes, and why we might make use of them. In addition, we saw how easy it was to add pagination like a boss in your app.

|