Posts

Showing posts with the label laravel

How to Build Nested Tree Structures for Parent-Child using The Laravel Eloquent Relationships

A few days back, working on a Laravel project I had to create an API returning the deeply nested Tree structure for the parent-child relationship of a given object entity - in this case, a Company. Although as an experienced PHP developer, I know how to build Nested Tree Structures for Parent-Child relationships using core PHP and SQL queries. But I was looking to, if Laravel Eloquent provides any efficient inbuilt method to fetch the Parent-child relationship with a deep nested tree structure. This led me to search on ChatGPT for the same. Here I want to give credit to ChatGPT which not only saved my time but also suggested a new way to get the desired result using Laravel Eloquent Relationships. Before I explain the solution for building a Nested Tree Structure, first let's understand it. Understanding Nested Tree Structures: A nested tree structure organizes data in a hierarchical manner, making it easy to represent parent-child relationships. Each node in the tree can have mult...

Laravel Middleware - Block IPs using Middleware class

When talking about securing the Laravel application, we might also want to restrict IP address to access our application, especially when we have Laravel setup as a Backend server providing data through APIs or web services. As of today, you might find no. of Laravel package providing IP address restriction functionality but I found it does not make any sense to install a package when it is achieved through only One class.  Yes! we can just do this with a simple middleware class.  Let me explain to you little details on how we can achieve it using the Middleware class. First, let's create a middleware class using the Laravel artisan command: php artisan make:middleware RestrictIPAddress It will create a new class in your Laravel project under the ` app/Http/Middleware ` directory. It will have a default empty method handle(), where you have to write your logic. Logic is simple, you just have to get the IP address from where the request is coming. here is the method...

Laravel Validation - Unique rule with multiple column and condition.

Today I am going to share, How we can use Laravel validation rule `unique`. If you have worked with laravel and familiar with laravel validation, you must know what does 'unique' rule checks. If not let me give here a brief about this. In simple words, It is similar to a unique key constraint applied to a column in the database table. 'Unique' rule check there is no similar value exists in the given database table column, to value matching with the provided input value. The general format of the rule is: unique:table //when input name is similar to column name OR unique:table,column OR unique:Model,attribute Examples: 'email' => 'unique:users' //here users is table name OR 'email' => 'unique:c,email_address' //can give custom column name. OR 'email' => 'unique:User,email'  //here User is a model class This is a very simple usage ...

How to Show Cookie Policy Consent or GDPR Popup in Laravel using Cookie.

Cookie in Laravel You can use the cookie in Laravel using the Helper method ` cookie() ` or method available with Request/Response Object or `Cookie` facade. $request->cookie('name'); OR use Illuminate\Support\Facades\Cookie; Cookie::get('name'); To get more clarity on how to use the cookie in Laravel with an example to show cookie consent popup on your Laravel website and hide/remove it once you accept the policy. Cookie Policy Consent Popup in Laravel First, create and a Cookie Consent popup as notification to the footer section of your Laravel main blade template file.  <div class="cookies-block">         <h5>This website uses cookies</h5>         <p>We use cookies to improve your experience and deliver personalised content. By using this website, you agree to our <a href="javascript:void()">Cookie Policy</a>.</p>         ...

Ways to Pass Parameters to Laravel job.

There are 2 ways to pass parameters to a dispatching job in Laravel. First is simply call dispatch() or dispatchNow() as per requirement on your Job class like calling a static method on a class: YourJob::dispatch(argument1, argument2, argument3); Second is simply pass arguments while creating an instance/object of Job class then pass the object to dispatch method(always available in the controller) like: $this->dispatchNow(new YourJob(argument1, argument2, argument3)); The arguments will be available in the constructor, can you assign them to class local variable properties and use anywhere in Job class. Let consider below example job: <?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; class Testjob imple...

Using Virtual Columns in Laravel - Accessors and Appends

What are virtual columns?  While developing any web application sometimes we need virtual columns - columns don't actually exist in our DB table, but we drive them from other columns. Why we need them? For example, generally, we use first_name and last_name  in the  users table. But sometimes we also need user's complete name i.e. full_name . There is no benefit of saving full_name in user table if we have already first_name and last_name in the table OR even if we have full_name  as a column in our table then there is no benefit of saving first_name and last_name in the table. So in a scenario like this, we prefer to use the virtual column. Using Virtual Columns in Laravel Considering the first case, in Laravel to get virtual column i.e.  full_name  from the users table will have Accessors. Accessors are methods defined automatically called by Eloquent when select is called on that particular column. Let take an example here: To get full_...

Laravel validation rule 'Exists' with secret feature.

In this post, I wish to share one secret feature of laravel validation rule 'Exists'. If you have worked with laravel and familiar with laravel validation, you must know what does 'Exists' rule checks. If not let me give here a brief about this. In simple words,  'Exists' rule check if a record exists in the given database table with column value matching with the provided input value. Format: 'exists:table,column' So this is what as mentioned in laravel documentation too. So looking at the format we understand that this rule is to validate given input value by check if any records exist or not for that particular input value matching with the given column values of the table. Let's take an example. 'booking_id' => 'exists:bookings,id' Here we are validating provided booking_id with exists, if records exist in bookings table matching the column id (in most of the cases but can use any column as per your requirement.) ...