Useful Laravel snippets

Every developer at some point in their work creates their style and identifies patterns in code that appear frequently. That helps them to create different and reusable code snippets that make their life easier and reduce development time. This time we want to draw your attention to the useful Laravel snippets the RIVO AGENCY developers use at work

abort(), abort_if(), abort_unless()

These helpers are usually used in the controllers and halt the execution of a code. They have several benefits if used:

  • they return the specific error if something went wrong
  • they return an error code
  • as far as, they are one-liners your code will be clean and with better readability

Laravel snippet

dd()

The dd() of the “die and dump” function is used when you need to dump the given variable and end the execution of a script. One of the greatest things about dd() is that it can take multiple parameters. That makes the debugging of complex scripts much easier. 

Laravel development

str_limit()

As practice shows, sometimes the simplest functions turn out to be the most useful. For example, how many times did you use this:

Lara

In this code, you have to check if the length of the text is not longer than 100 characters then shorten and concatenate it if it is true or just reflect it in full. 

With str_limit() things are getting easier. When you use it, the code will be more readable and simpler to understand. Look at the example below and see the difference.

Development company

collect()

One of the most useful functions is the collect() function. When you use it creates an instance of the Collection class from the given array. 

Laravel development company

You might think why do we need it, but a Collection is a kind of pumped array. To understand everything better let’s take a look at the operations that we do every day with our arrays:

  • we iterate over items to do some action
  • filter items 
  • getting sums of the items

You can do them a lot easier if you use Collections. When we use a regular array we use forEach loop to iterate over every item and put the item we want. 

custom software

But when we use Collections we can do everything with just one line of code. 

software development

As the result, you will receive a new Collection with all the matches. 

custom software

Another example is the usage of the filter() method.

Laravel software

Here, the filter() method accepts a callback function and returns all the users with an age lesser than 25. As a result, it returns a new Collection with all results.

"Ownership" trait for the models

Sometimes when you need to perform an authorization check, you have to verify if a certain model owns another model. In cases when you need to do that a lot you can add an “ownership” trait to your models:

useful snippets

Read also: “How to maintain an effective teamwork”

Increase smoothness with whereBelongsTo()

If you want your app to work a bit smoother, one of the best tips you can use is to utilize the whereBelongsTo() method instead of the usual where().Laravel code

whereBetween() method

In case you want to get data between two time frames and from a particular column, you can use the whereBetween() method to make it easier. 

Laravel programming

prependKeysWith()

Using the prependKeysWith() method you can append the prefix in array keys as in the example below.

Laravel developer

Use a closure instead of a rule object

Here is another useful validation tip. Use a closure instead of a rule object when you need a one-time custom rule in your app.

The closure will receive the name of an attribute, its name, and a callback function that should be invoked if the validation fails.

closure

Avoid adding duplicates when storing database notifications

When you store database notifications you can avoid adding duplicates, so users could receive only unique content. In order to do that if you add a hash of the content and then enforce a unique constraint on the table.

database notifications

Concluding

All the above Laravel snippets we use on the daily basis in our custom software development company. You can also use them to enhance your work and become an even better Laravel developer. 

Creating good decisions and implementing them in code helps us keep our clients happy and deliver exactly what they need for their businesses to grow.

Discover Discover

Contacts





Let’s make something that matters