getOrPut feature was introduced in Laravel 8.81 and it’s a pretty amazing oneliner. getOrPut helps you to get an existing key or put the value if it doesn’t exist and return the value on a collection. As of now you want to have three lines of code to achieve this, but getOrPut changes everything. if…Continue Reading
Laravel
What is the opposite of has() method in Laravel?
We all know the has() method helps list a number of records with at least a related record in Laravel Eloquent. For example, below we can get all posts that have at least one comment. $posts = App\Models\Post::has(‘comments’)->get(); But what would be the opposite of this? How do we get a post that has zero…Continue Reading
Laravel: Download files to storage from SFTP
This post will discuss using Laravel to read and download files from a remote server via SFTP. For this purpose, we use a third-party package to connect to sftp and download the files to the storage folder in Laravel. The first and most important step is to install and configure the league/flysystem-sftp in the Laravel…Continue Reading
Laravel Eloquent- Where Condition with Two Columns

Did you ever think about how to write where condition with two columns(fields) in Laravel Eloquent? In the first place, this is possible in Laravel using the function . For example, you have two fields named sub_total and grand_total, and you need to find all the orders have the same sub_total as grand_total. So you…Continue Reading
How to Add Foreign Key in Laravel Migration?
This tutorial shows how to add a foreign key constraint to a field in Laravel in the migration file. As we know already a foreign key is a field that is used to establish the relationship between two tables via the primary key. This example starts with creating a migration file in Laravel and shows…Continue Reading
What is Str::finish() in Laravel ?

In this article, you will learn about Str::finish() in Laravel. The next question is how to incorporate Str::finish() into your project. If a string does not already end with the given value, the Str::finish method adds a single instance of that value to it:
How to Substract and Add Hours in Laravel Using Carbon?

In today’s article we will illustrate how to use the Carbon library to add and subtract from a given time in Laravel. As you know already, Laravel is capable of doing pretty much anything. This is tested with Laravel7, 8,9 versions. Here you need to take note of Carbon – Well known PHP API for…Continue Reading
How to Get Last Week Data in Laravel using Carbon?
In today’s tutorial, we are showing how to get last week’s data in Laravel using Carbon. In other words, you need to pull all the orders received in the last week. In this case, last week does not mean the last 7 days, but it’s a full week starting from Monday to Sunday. For example,…Continue Reading
Differences between query() and input() in Laravel?

You may be wondering what’s the difference between method and method in Laravel. It’s mentioned straight away in the laravel docs. While the input method retrieves values from the entire request payload (including the query string), the query method will only retrieve values from the query string: The important aspects and the real difference between…Continue Reading
Raw Queries in Laravel

Eloquent is the answer for all kinds of database operations in Laravel. As a matter of fact, which is a great solution for interacting with databases. But if we have complicated business logic/ complex MySQL queries, oftentimes we need to write raw queries in Laravel. A key concern about the usage of raw queries in…Continue Reading
Laravel Eloquent Select Column as Alias
Did you ever think about using an alias in the select columns while using Laravel Eloquent?. We cannot say it’s very Laravel specific, rather a MySql feature. Basically, when you join two or more tables in MySql, chances are very high that you will have the same column names multiple times in the result. This…Continue Reading
Laravel 8 Eloquent updateOrCreate() Example

In this post, we will explain about Laravel Eloquent updateOrCreate() method and its importance in our code. It’s very common that we have database operations in our application, like inserting customer details and updating that customer details in a later stage, or deleting customer details. For example, you have a function that is responsible for…Continue Reading
What does Model::unguard() do in Laravel?

does temporarily disable the mass assignment protection of the model, so you can create records without worrying about the fillable. It’s not recommended to it for the users, but only for specific task such as seeding etc. For example, We have a table in our application. $table->double(‘name’); $table->boolean(‘status’)->default(false); And in the model protected $fillable =…Continue Reading
How to Export CSV file in Laravel Example

Exporting data to CSV is one of the many features most applications should have. Sometimes it would be a report for your marketing or finance team or a list of products in your inventory.
How to create Your Own Custom Log File in Laravel
Laravel is a well-known PHP framework for web artisans. In today’s tutorial, we will discuss how to create our own custom log file in Laravel. Logs are a very important part of an application as it records user journey in simple text files, so the administrator can look into these files if there is any…Continue Reading
Laravel Artisan command for generating Request validation Class

The command can be used to generate new request validation request classes. You can input the name of the class to generate the class. The name will be used as the name of the class and the file. Follow the example below to generate a Request class in Laravel. php artisan make:request ProductPostRequest A new…Continue Reading
Bind vs Singleton in Laravel. Which one to use, and when?

In today’s tutorial, we are discussing bind() and singleton() methods in Laravel. In general, you might have encountered this a lot while you programming in Laravel based apps. We use in the event of reusable classes or objects ( the object is created each time when it is called). So each instance of the class…Continue Reading
Attribute Casting with Laravel
Laravel is well known MVC framework written in PHP programming language. Over the years, it became the next tool in your belt for a plethora of programmers. When we work with MVC, we have to deal with models and attributes as this is the layer that makes contact with our database. Oftentimes, we need to…Continue Reading
How To Get HTTP Hostname In Laravel ?
In today’s tutorial, we discuss how to get the HTTP hostname in the Laravel project. The first thing to remember, we have helper that can be used anywhere in the Laravel projects such as blade templates, controllers, models, etc. So this function is equipped with two methods as shown below to get the HTTP hostname…Continue Reading
How to call static function from Helper in Laravel blade file?
We have already discussed creating custom helper functions (Global Function) in Laravel in a previous article. So today’s article we discuss a different approach to achieve the same feature, and this function can be called from the blade template as well. Step 1. Add this code in your composer.json file located in the root directory…Continue Reading
Laravel WhereHas() Eloquent Example
WhereHas() is a Laravel eloquent method, which helps to define additional query constraints on your queries, such as filtering your books with some author name. For example, return all the books if it matches an author named Charles Dickens. But whereHas() often confused with some other terms in Laravel, has() and with(), which is very…Continue Reading
How to Rollback migration in Laravel ?
When you check the migration table in Laravel, you can see a batch number with every record. So, when you run the rollback command, it rolls back each migration that was part of the last migration. php artisan migrate:rollback The above command will roll back the last batch of migrations which may include multiple migration…Continue Reading
Laravel str is() Function With Example
In today’s tutorial, we will show you how to use string is() function Laravel. Basically, this function determines if a given string matches a given pattern. Asterisks can be used to indicate wildcards. Note: we can use this function as Str::is() or is(), both are correct as it’s a helper function, and it can be…Continue Reading
How to use Faker with Laravel ?
As the name suggests Faker is a PHP library that generates fake data for you. Why do we need fake data? Good question, we need fake data to simulate a development environment, which looks very close to real data. Sometimes developers need to have a bulk amount of dummy data, to test a feature like…Continue Reading
How to Add Foreign Key in Laravel Migration?
Today’s tutorial shows how to add a foreign key to a MySql table field in Laravel migration. I am sure you are in the right place to start with the migration and foreign key constraints in Laravel. As we discussed earlier, the foremost purpose of the foreign key is to provide referential integrity between parent…Continue Reading
Check for the application environment in Laravel views or controller
Often times you will come across to check what is the current environment in your application. I am talking about if it’s in production or development or any other state that you defined. For example, you may need to show Google ads on your website when it’s in production mode. Indeed, you need a directive…Continue Reading
How to Get a List of Registered Route Paths in Laravel 8?
There are many ways we can get the list of registered routes in Laravel. I would prefer the below two methods.
Two ways to set default DB column value in Laravel
Sometimes your project needs to set default values to some fields if it’s not set from the form/another source. In Larvel there are two ways to set default values to the DB column.
How to constrain a route parameter with a given regular expression globally in Laravel?
How would you make sure a route parameter is always a numeric value. Most of the time an parameter will be an integer value. So today’s tutorial we will show how to globally constrain a parameter to a regular expression.
Using Laravel’s dd (dump and die) function in your application
One of the common debugging option still remains in PHP is to print the values in the browser using or .