February 27, 2021 — Sometimes, when working with HTTP responses, you might want to retrieve the entire response as a Laravel collection to make further manipulations.
February 23, 2021 — When working with Mail notifications in Laravel, you’ll often be in a situation where you would want to render something based on some condition.
February 21, 2021 — If you have worked with a Large scale Laravel application, there might be a great chance that you would have to use JSON columns to save some information in your tables.
February 7, 2021 — The fluent string operations were introduced back in the release of Laravel 7 which offered a more object-oriented, fluent string manipulation library built on top of
Illuminate\Support\Str functions. To use this fluent API, you’d need to use the
Illuminate\Support\Str::of method which creates a fluent
January 24, 2021 — There are certain operations that can have a high probability of failure. For instance, sending a request to a third-party API. You’re not sure whether it will work every time flawlessly because it’s not in your control. If the API endpoint you’re hitting is not responding, you might end up with an exception straight away.
January 21, 2021 — With the latest release minor of Laravel, i.e. v8.23.0, a really interesting method has been introduced in Laravel’s query builder for situation where you want to get the only record for the matching criteria. But if there are more records for this criteria, there should be some sort of exception.
January 19, 2021 — While working on your codebase, be it developing features for your application or fixing bugs, we all tend to get a little bit demotivated sometimes in the middle of those sessions. Because after all, we are all humans and we all need some kind of inspiration every now and then.
January 18, 2021 — When you are using Laravel queues, you might bump into the situation where you want to prevent overlapping of jobs that handles sensitive information. This could be to provide data integrity of important information such as the user’s wallet balance.
January 11, 2021 — Here’s a little tip about the artisan command that generates the controller. So, if you’ve used the
make:controller command with the
--resource option, it will generate controllers with create, read, update, and delete (“CRUD”) actions in them.
December 30, 2020 — When working with polymorphic relationships in Laravel’s Eloquent, it’s important to specify the “type” of the related model. You can do this by using
December 16, 2020 — Sometimes, it would be convenient if you get details regarding things right in your terminal. Take for instance, all your scheduled commands that you have defined in your application’s
schedule method like so.
December 15, 2020 — There is no denying the popularity of PHP. It has been a constant force in the web development world since its release in 1995. And now, thanks to Laravel, it is still going as strong as ever!
December 14, 2020 — Up until now, if you would need to delay notifications, you could do it by chaining the
delay method onto your notification instantiation like so.
December 9, 2020 — Imagine you just bought a new computer and the next thing you want to do is create a brand new Laravel application in your brand new machine.
December 3, 2020 — Sometimes, there might be a case where you would want some of the code that lies outside of the transaction to be executed only when the corresponding transaction commits successfully and if in any case, it’s a rollback, that code shouldn’t be executed.
November 25, 2020 — Laravel keeps getting better on each of its major releases. But sometimes, it’s the minor and patch releases that take the cake by introducing some lesser-noticed features.
November 22, 2020 — You might face a scenario where you’d want to restrict a user from submitting a form within a certain time limit. i.e to prevent spamming by users.
November 18, 2020 — The Laravel Telescope package is great for debugging your application when you’re working on your application in the
local environment. It provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps, and various other things in this beautiful interface.
November 16, 2020 — MySQL’s EXPLAIN statement is a handy tool if you want to take a quick glance over queries and to make out what’s wrong with them. Essentially,
EXPLAIN is used to obtain a query execution plan (that is, an explanation of how MySQL would execute a query).
November 11, 2020 — When Laravel 8 released, It came with a lot of new features & improvements but along with it, the Laravel team has also introduced Jetstream for application scaffolding which comes with all the bells and whistles in-built such as login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management.
November 6, 2020 — Sometimes, there comes a scenario where you want to loop over a collection or array and include Blade views based on the iteration of that collection/array. You can use
@include for this purpose but there’s a handy way using which you can simplify this process.
November 2, 2020 — Sometimes, all you want to do is generate temporary URLs for files that you have stored on your AWS S3 bucket. For instance, you would want to use this to prevent hotlinking of images.
October 23, 2020 — If you’ve worked with Laravel, you might be aware of the fact that all the multi-result sets returned by Eloquent are instances of the
Illuminate\Database\Eloquent\Collection object. So, for instance, if you have a
User model, you can get all the users like so.
October 20, 2020 — It would be useful sometimes when you’re not sure if you want to insert or update records and you’d let the system decide that. Laravel comes with one such feature that lets you do just that.
October 14, 2020 — Laravel Envoy is a great tool if you want to create deployment flow for your application or even something as small as writing a script to pull the latest code and deploy the application right away.
October 12, 2020 — You know if you want to pass data to the views, you can do it while defining routes or from controllers like so.
October 5, 2020 — In one of my articles, I’ve explained how you can extend class behavior using macros in Laravel.
October 3, 2020 — One of the standard ways to resolve dependencies in Laravel is by using service container. Essentially, if you want to inject something which can be swappable anytime, you can do this by “biding” that into the service container.
September 24, 2020 — Laravel, as a framework, has grown slowly but strongly since its inception and so is its ecosystem. The ecosystem here is all the packages/products that are built by the Laravel team which revolves around the original Laravel framework. This includes open-source packages and premium/paid products.
September 23, 2020 — One of the handiest features of Laravel is the ability to dispatch Closures to queues. This is because you don’t always need to create a job class for simple tasks such as sending emails.
September 18, 2020 — Laravel 8’s class-based model factories are great and I’ve written an entire article about what they are and how they are more feature-rich and useful than the traditional factories that we used to write prior to Laravel 8.
September 16, 2020 — One of the most prominent changes in Laravel 8 is the (kind of) new way of defining controller routes. Before Laravel 8, if you wanted to define a route, you could do it like so.
September 14, 2020 — In the previous versions of Laravel, up to 7.x, when you wanted to rate limit certain routes, you could do it by assigning the
throttle middleware to the route or group of routes like so.
September 12, 2020 — The model factories in Laravel are a great way of seeding some fake data in the database for testing purposes which I’ve discussed in this article. While the previous implementation was great, Laravel 8 has given model factories a major overhaul and it makes them even better!
September 11, 2020 — Apart from adding support for customizable maintenance mode views, Laravel 8 has also simplified the process of accessing the Laravel application in the maintenance mode.
September 10, 2020 — When you put your Laravel application on maintenance mode using the
php artisan down command, the default view for the same looks like following.
September 9, 2020 — With the release of Laravel 8, you can now render Blade components dynamically using a built-in component called
September 1, 2020 — In one of my articles, I’ve described how you can add different behavior to some of the classes in Laravel using macros. To give you a primer, Laravel ships with this trait called Illuminate\Support\Traits\Macroable using which you can add additional methods to the class in hand (which is using the
Macroable trait) at run time.
August 26, 2020 — You know it’s always a good idea to have something at your disposal using which you can improve the performance of your applications. When it comes to web applications, one of the many things that power them is database queries and that’s why it’s no surprise that you may want to optimize those and make your application a little faster!
August 24, 2020 — You might be aware of the typical way of defining model relationships in Laravel Eloquent. i.e inside of the models itself.
August 20, 2020 — There comes a time when you want to fetch the number of records for a child relationship along with the main Eloquent query.
August 17, 2020 — One of the many ways using which you can insert records (or seed your database) is by using the seeder classes which extend a base class called
Illuminate\Database\Seeder in Laravel.
August 10, 2020 — When writing MySQL queries, if there are more than two where conditions, you’d want to group certain constraints/conditions to avoid unexpected behavior. For instance, check the following query.
August 8, 2020 — As I described in this article on how you can create anonymous components in Laravel, we can create an
<x-alert> component with the following content.
August 5, 2020 — Often you’d come across a scenario where you’d like to check the application environment and based on that you’d like to render things.
July 31, 2020 — Laravel’s Blade template engine is great and ever-evolving. There are some features in Blade, which if used appropriately, can make your codebase look more organized and recognizable.
July 20, 2020 — Sometimes, you want to load some stuff based on the fact that the certain service provider is loaded in the
providers array of
config/app.php or not.
June 18, 2020 — Sometimes, it’s useful to know if somethig has been changed in the model since it’s been loaded. For instance, you would want to check if the
is_published attribute of the
Post model is set or not before publishing the post further.
June 11, 2020 — It’s helpful sometimes when you have a file and it doesn’t have extension attached to it but you want to know the extension of the file regardless.
May 28, 2020 — The usual way of validating request attributes in Laravel is by making
Illuminate\Support\Facades\Validator instance with your static rules that never change like so.
May 20, 2020 — Laravel comes with a multitude of ways to validate request parameters. For instance, you could use
Illuminate\Http\Request’s validate method where you can specify all the fields that you would want to get validated inside controller’s action. You can do it like so.
May 7, 2020 — Lately, if you’ve been working on some functionality and you want to extract it as a Laravel package, you’d like to first test it locally. And if everything is well and good, you’d then publish it to the packagist.org.
April 29, 2020 — Facades, in Laravel, is basically a way of using Laravel’s classes without injecting them into the constructor of the class in which you want to use them.
April 27, 2020 — Laravel Blade comes with many in-built directives such as
@json and several others, all of which have a certain purpose attached to them.
April 24, 2020 — The Observer design pattern is one of the twenty-three well-known “Gang of Four” design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.
April 23, 2020 — Sending emails in Laravel is a breeze. All you have to do is, configure your default mail service in
config/mail.php and it’s related configuration in
April 14, 2020 — The new Blade components in Laravel 7 are great. They provide an easy way of defining blade components in a Vue-like tag aliases. One of the nice features of Blade components are slots.
April 7, 2020 — Blade component tags are a useful additions to the latest version of Laravel i.e. version 7. In Laravel 7, Blade components have been overhauled to allow tag based rendering, attribute management, component classes, inline view components, and more.
April 2, 2020 — Often times, there comes a sceanrio where you’d want to alter the column through which you’ve setup your query to be sorted from. So, for instance, you’ve the following query.
March 18, 2020 — If you want to validate request fields in Laravel, you could use
Illuminate\Http\Request’s validate method where you can specify all the fields that you would want to get validated inside controller’s action. You can do it like so.
March 12, 2020 — Artisan commands in Laravel are truly a blessing. I mean you could create just about any files be it controllers, models, middleware, provider by knocking a simple command from the CLI. For instance, if you want to create a model named
Post you could just fire the following command…
March 10, 2020 — Laravel 7 has been a major version that’s been released last week and it comes with the host of features and improvements to the Laravel framework. One of the improvements being the new fluent string operations API. The goal of these API is to provide more flexibility and readablity to the regular string operations in Laravel by chaining array of string manipulation methods.
March 5, 2020 — I’ve discussed about Route Model Bindings of Laravel in length in this article previously. If you’re not aware about the feature, I’d recommend you check that article first and come back here again.
February 26, 2020 — Working with Laravel Eloquent, we always had this ability to check
null fields when builing queries on models. For instance, if you want to check if
email_verified_at field is
null or not, you’d check it like so.
February 16, 2020 — Sometimes, you might want to convert some of the request parameters to boolean. For instance, take a checkbox field. Unless and until, it hasn’t been checked it won’t be passed through to the request. In such a case, it would be beneficial to convert such inputs to boolean.
view-js.blade.php which wraps the JS code within
<script> tag and a CSS blade file called
view-css.blade.php which wraps the CSS code within
<style> tag. And all you want to do is add these to an another blade file called
January 30, 2020 — Laravel’s Blade is a great templating system that blends with Laravel’s ecosystem very well. Setting some data to the view is a breeze and rendering those data into the template is ever so easy. For instance, if you would like to share some data from a controller’s action to a view, you’d do like so.
January 29, 2020 — Sometimes, you might want to attach additional attributes to the original request that has made to the Laravel application. For instance, you want to put your own check in the middleware to see if the user is permitted to access the page or not, and pass that “check” by attaching it as an additional attribute to the request.
January 23, 2020 — When designing UI elements such as zebra strips in the tables, you’d need to identify if the loop iteration is even or odd and based on that you’d set the backgroud color of the row.
January 22, 2020 — There are basically two ways of using the
auth middleware to authenticate the routes in Laravel.
January 20, 2020 — Laravel’s
Blade::if() method already comes with the following directives in order to check various conditions in blade template files.
January 15, 2020 — Sometimes, it’s convenient when you could get to access class objects as arrays. For instance, the time when one of the class properties is of type array and you want to manipulate it just like you’d do with an array without exposing it during object creation. I’ve included a real world example of where this could be useful, at the end of the article. So, read on!
January 9, 2020 — There comes a scenario in your app where there are some fields in your form that are dependent on an another field’s state.
December 12, 2019 — The best thing about working with Laravel is finding an alternate way to do same old boring stuff every now and then. I came across something called as “Higher Order Messages” while working with Eloquent recently. The feature has been in Laravel since its v5.4.
December 1, 2019 — There exists four types of relationship associated between models/entities. And they are: One To One, One To Many, Many To One and Many To Many. We’re specifically going to talk about Many To Many relationship and most importantly
sync helper methods that are provided in Laravel in this article.
November 6, 2019 — If you’re looking for a quick way to drop all the tables, their types and views and if you’re using Laravel 6.x, you can use this little artisan command called
db:wipe in order to do the same.
November 2, 2019 — In this article, I’m going to discuss about the feature in Laravel using which you can extend the functionality of certain Laravel’s core classes without even touching the original codebase. Or in other words, adding methods to the class dynamically.
October 9, 2019 — If you’ve used some well creafted web application, such as GitHub for instance, you might’ve noticed that upon saving sensitive information such as settings or payment details, it asks for the password confirmation before performing the action. This adds the extra layer of security and certainly a nice-to-have feature.
September 11, 2019 — If you love the Laravel as a framework or more specifically its Eloquent ORM like I do then there’s a great chance that you’d like to use this great ORM in your non-Laravel PHP apps as well. And the good news is it’s rather easier than you might think. Laravel provides a standalone pacakge called Capsule which you can use in your own project. It’s a full database toolkit for PHP, providing an expressive query builder, ActiveRecord style ORM, and schema builder. It currently supports MySQL, Postgres, SQL Server, and SQLite.
September 4, 2019 — Laravel team has recently released
v6.0 of the framework and with this they have added a bunch of exciting new features. Among which, I’m going to talk about Lazy Collections in this article. In Laravel,
Illuminate\Support\Collection class provides a fluent, convenient wrapper for working with arrays of data. In face, all the Eloquent queries are always returned as
LazyCollection essentially extends the features of the
Collection class. Let’s talk about them in detail.
August 27, 2019 — In this tweet, Freek Van der Herten explains how to process chunk of records from a model instead of loading all of them at a time which can help using less memory. Learn more about it in the article.
August 26, 2019 — As backend developers, to make our web application full proof we need to make sure our application is running as efficiently as possible all the time. Otherwise, everyone using your database will suffer from slow performance. API limiting, which is also known as rate limiting, is an essential component of Internet security, as DoS attacks can tank a server with unlimited API requests.
August 3, 2019 — To understand, how dependency injection works in Laravel, let’s just get to know what dependency injection actually is, in software engineering.
July 12, 2019 — If you want to check if the eloquoent model has certain relationship or not and fetch the results accordingly while querying upon it, you can do this by two approaches in the Laravel Eloquent.
July 6, 2019 — When you’re building an Laravel application which uses APIs at some point, it’s no brainer to use Laravel’s resource classes as it provides the transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application’s users.
June 16, 2019 — Laravel’s Eloquent ORM comes with this unique set of features called “Scopes” which can be used to re-use some of the query constraints onto all the queries of a certain model. This can be useful to minimize the code while writing the query and is an easy way to make sure every query for a given model receives certain constraints.
May 5, 2019 — Every robust web app has this feature where you’re provided with a setting through which you can choose to logout from all the devices you’ve been logged in from previously, except the current one. This is certainly a nice security feature that you can provide to your user without them even asking for it and if your app is built on top of Laravel 5.6, this comes out-of-the-box.
April 29, 2019 — There’s this neat feature in Laravel where you can validate model IDs that have been injected into the route by injecting model instances directly at route level.
April 15, 2019 — Laravel’s Eloquent is without a doubt a great ORM exists right now. It has some of the features which makes it ahead of its competitions. One such feature the ORM provides is called “Attribute casting”.
March 28, 2019 — There comes a time when you want URLs which are public but still you want some kind of authentication onto the same. For instance, you’d want such kind of URLs to enable invite-only registrations for your applications, where you’d send a URL to the user which that specific person only can access and register. In such scenarios, signed URLs can come in handy which are the special kind of URLs that have a “signature” hash appended to the query string to verify that the URL has not been modified since it was created.
March 26, 2019 — There exist this neat feature in Laravel using which you can add attributes in the models that do not have a respective column in your database.
February 9, 2019 — While developing applications(not necessarily with Laravel), you come across the situation where you need to implement certain functionality. In such cases you shouldn’t want to reinvent the wheel if the functionality is been implemented by someone as a package, accepted well by the community and is ready to be consumed. It’s a no-brainer. I use to follow the same approach and today in this article, I’m going to list out some of my favorite Laravel packages which have made my experience with development in Laravel delicious.
January 9, 2019 — When working on a project, there comes a time when you need to automate certain tasks. In such cases, it is better to use a CLI tool which can be run from the command prompt because using a web APIs for certain tasks is cumbersome.
December 21, 2018 — There comes times when you wouldn’t want your end users staring at white screens or that intimidating loaders for a long time. For instance when user registers to your site, you must have configured your website to send a welcome email or a confirmation email upon registering. So, that operation should be snappy and for that purpose you’d actually want to reduce the amount of time your app takes while sending the email to the user. Laravel Queues comes to the recue for achieving such kind of time consuming tasks.
August 23, 2017 — Accessors and mutators allow you to format Eloquent attribute values when you retrieve or set them on model instances. I’ll explain how you can you use them into your app.
August 17, 2017 — Today, while working with one of my projects(which is built on top Laravel) I bumped into the situation where I needed to get associated model’s data for one of the models. So here, I had two approaches in Laravel to accomplish this which are basically called Eager Loading: