How are AI tools helping solve real, complex, and time-consuming problems faster?

Nowadays, almost everyone is either using AI tools or thinking about incorporating them into their daily work. At the same time, many professionals are still hesitant, worrying that AI might replace their jobs. While those concerns are understandable, I believe the bigger risk is ignoring these tools altogether. From my experience, AI isn't replacing engineers—it is helping engineers work faster and focus on solving more meaningful problems. Like any other tool, its value depends on how effectively we use it. The more we learn to collaborate with AI, the more productive and efficient we can become. Here are two real examples from my recent work where AI tools helped me solve complex and time-consuming engineering problems significantly faster. AI helped me audit and upgrade my project's tech stack by checking dependency compatibility and flagging version-specific issues in advance. Instead of manually reviewing every package and verifying the latest compatible versions, I was ...

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 multiple children, creating a flexible and intuitive structure.

Setting up the Database:
Start by setting up a table in your database to store the companies. Include a parent_company column to establish the parent-child relationship. This column will contain the ID of the parent company.

Defining the Model:
Create a model for your company table using Laravel Eloquent. This model will enable you to interact with the database and perform operations on the data. 

class Company extends Model
{
protected $fillable = ['name', 'parent_company'];
}

Building the Tree Structure:
To construct a nested tree structure, utilize methods provided by Laravel Eloquent. For instance, you can use the `hasMany` relationship to define the children of a company:

class Company extends Model
{
public function children()
{
return $this->hasMany(Company::class, 'parent_company', 'id');
}

public function descendants(): HasMany
{
return $this->children()->with('descendants')->withCount('users');
}
}

Retrieving the Tree with Children:
Now, fetching the entire nested tree becomes straightforward. Suppose you want to retrieve all companies with their respective children:

$companies = Company::with('children')->get();

Retrieving the Tree with Descendants:
Fetching the entire nested tree along with descendants becomes a simple eloquent query:
$companies = Company::with('descendants')->get();


So this is how using Laravel Eloquent, I have easily built and managed nested tree structures for parent-child relationships. This approach enhances the readability and maintainability of your code, providing a solid foundation for handling hierarchical data.

I hope this will help you learn the basics of creating a nested tree structure in Laravel Eloquent. Feel free to explore additional features and customization options to suit your specific needs. 

Comments

Popular posts from this blog

Differences between json and jsonb - JSON data type in PostgreSQL

Laravel Validation - Unique rule with multiple column and condition.

Using Virtual Columns in Laravel - Accessors and Appends