Skip to main content

Command Palette

Search for a command to run...

Laravel Basics

Updated
5 min read
Laravel Basics

How to kick start a Laravel project?

First we need to go Laravel official website and open getting started page.

If you’re creating this application for the first time you need to install PHP, Composer and Laravel installer. In addition, you should install either Node and NPM or Bun so that you can compile your application's frontend assets.

If you already have PHP and Composer installed, you may install the Laravel installer via Composer:

composer global require laravel/installer

After you have installed PHP, Composer, and the Laravel installer, you're ready to create a new Laravel application. The Laravel installer will prompt you to select your preferred testing framework, database, and starter kit:

laravel new example-app

Once the application has been created, you can start Laravel's local development server, queue worker, and Vite development server using the dev Composer script:

cd example-app
npm install && npm run build
composer run dev

Once you have started the development server, your application will be accessible in your web browser at http://localhost:8000.

Databases and Migrations

Now that you have created your Laravel application, you probably want to store some data in a database. By default, your application's .env configuration file specifies that Laravel will be interacting with an SQLite database.

During the creation of the application, Laravel created a database/database.sqlite file for you, and ran the necessary migrations to create the application's database tables.

If you prefer to use another database driver such as MySQL or PostgreSQL, you can update your .env configuration file to use the appropriate database. For example, if you wish to use MySQL, update your .env configuration file's DB_* variables like so:

DB_CONNECTION=mysqlDB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

If you choose to use a database other than SQLite, you will need to create the database and run your application's database migrations:

php artisan migrate

Understanding Class Accessors and Traits in Laravel

🔹 What is a Class Accessor?

In Laravel, a class accessor refers to a method defined within an Eloquent model that automatically transforms or formats attributes when they are retrieved from the database. Instead of manually applying formatting or transformations every time you access a value, accessors centralize this logic within the model itself.

Accessors are particularly useful for maintaining clean and readable code. They ensure that data is consistently presented in the desired format across your entire application. By using accessors, developers avoid repeating the same transformation logic in controllers, views, or other parts of the codebase. This makes applications more maintainable and helps enforce business rules at the model level.

In short, accessors provide a way to control the output of model attributes, ensuring that values are always displayed in a standardized and predictable manner.


🔹 Why Use Traits in Laravel?

A trait in PHP and Laravel is a mechanism for reusing sets of methods across multiple classes. Since PHP does not support multiple inheritance, traits offer a way to share functionality between unrelated classes without duplicating code.

Laravel itself makes extensive use of traits. For instance, features like soft deletion, notification handling, and authorization are implemented as traits that can be easily added to models or classes. This approach encourages modular design, where specific pieces of functionality can be mixed into a class as needed.

The primary benefit of traits is code reusability. Instead of writing the same methods in different classes, developers can define them once in a trait and then use them wherever required. This reduces redundancy, keeps code organized, and makes it easier to maintain.

Traits also provide flexibility, as a single class can use multiple traits at the same time. This mimics some advantages of multiple inheritance while keeping PHP’s class structure simple and manageable.


🔹 Multiple Inheritance in Laravel

PHP, and by extension Laravel, does not support multiple inheritance directly. A class can only extend one parent class at a time. However, traits fill this gap by allowing a class to “inherit” behavior from multiple sources.

Through traits, Laravel achieves the effect of multiple inheritance without introducing the complexity and conflicts that often come with it. Developers can combine multiple traits in a single class, gaining access to diverse functionality in a clean and controlled way.


✅ Final Thoughts

  • Class Accessors help standardize how model attributes are presented, ensuring consistent formatting across your application.

  • Traits promote code reusability and modularity, offering a way to include shared behavior across multiple classes.

  • While multiple inheritance is not possible in PHP, traits provide a powerful alternative, allowing Laravel applications to remain clean, flexible, and maintainable.

  • Accessor: Transform attributes automatically when retrieving from a model.

  • Trait: Reusable piece of code across classes (alternative to multiple inheritance).

  • Multiple inheritance: Not allowed in PHP, but Traits act as a replacement.

  • PHP does not support multiple inheritance directly.

  • A class can only extend one parent class.

  • But, Traits provide a workaround: a class can use multiple traits at the same time.

  • Single Inheritance (extend one class)

  • Multiple Traits (reuse many behaviors).

🔹 What is an Unsigned Attribute?

In databases (like MySQL, which Laravel commonly uses), an unsigned attribute means that a numeric column cannot store negative values.

  • Normally, an INT column can store both negative and positive numbers (for example, -2,147,483,648 to 2,147,483,647).

  • If you mark it as UNSIGNED, the column can only store zero and positive values, but the range of positive values doubles (for example, 0 to 4,294,967,295).


🔹 Why use UNSIGNED?

  1. Logical correctness → Some data should never be negative, such as:

    • IDs (Primary Keys)

    • Age

    • Price

    • Quantity

  2. Extended positive range → By disallowing negatives, the database allows a wider positive range.

  3. Consistency and validation → It prevents accidental insertion of invalid negative values.


🔹 Unsigned in Laravel Migrations

When defining migrations in Laravel, you can specify unsigned() on numeric columns.
For example:

  • unsignedBigInteger → commonly used for foreign keys and IDs.

  • unsignedDecimal, unsignedInteger, etc.