How to use rate limiting in Laravel

It is almost always a better idea not to use IP as part of rate limiting in Laravel. The main reason for that it can be easily changed with proxies and VPNs. Did you know that there is an elegant syntax for multiple rate limiters in Laravel? Here they are: This code will not help […]

How to use whereRelation method in Laravel

Laravel tip. You can use the whereRelation method (introduced in Laravel 8.57+) instead of whereHas when filtering by a simple where statement. The only downside is that you cannot use scopes, whereRelation is suitable for only one simple “where” statement. The merit of using whereRelation is shorter and more readable syntax, and in terms of […]

How to render SVG with Blade components in Laravel

Laravel is a very flexible framework, and you can reutilize your Blade components when it comes to SVG rendering. You can use {{ $.attributes }} in the component itself to pass custom attributes like (e.g., “class”). Just look at the following syntax: File structure: Helpful tip If you see the use of href="#!" and have […]

How to use conditions in Laravel Eloquent

Eloquent is a brilliant ORM – you can use conditions in relationships. In this example, we have multiple users. In the next step, we set a relationship called admins which extends the user relation with a condition. So easy! But you have to be mindful when creating a user via admins relationship. You will need […]

How to push jobs to the queue in Laravel (Cheat sheet)

Here is the cheat sheet on the ways you can push jobs to queue in your Laravel application: If you are configuring Laravel Queues with AWS SQS, and you want to queue a job later than 15 minutes in the future, you may want to use the Laravel Haystack package. You can also use the Bus::chain method […]

How to check if a string is valid JSON in Laravel

It is very common when you have to check whether a string is a valid JSON or not. An example of Str::isJson in Laravel In Laravel, you may want to use Str::isJson method as follows: These are examples from the official documentation: What is JSON for? JSON is a data format that helps exchange data […]

How to register long query callback in Laravel

If you are running your Laravel app in production mode, you may want to register long running queries, which is very useful for debugging purposes: You can also sort these long queries by their duration of execution, so that the longest running ones are easier to see: Via @realstevebauman.

How to measure the performance with dd() in Laravel

We now have quite a good option with the new implementation of dd() method. So, since Laravel 9.32.0 you can easily test the performance of any part of your Laravel application. Just add new “Benchmark” class and you will be able the time it takes (in milliseconds) for the certain callbacks to complete: You can […]