- 2 years ago
- Zaid Bin Khalid
- 1,073 Views
-
2
In Laravel, the database plays a crucial role in storing and retrieving application data. Laravel provides a powerful database abstraction layer, allowing developers to interact with various database systems using a consistent API. Migrations, on the other hand, are a way to manage database schema changes in a structured and version-controlled manner. Let’s explore Laravel’s database and migration concepts in detail:
Database Configuration
Laravel’s database configuration can be found in the config/database.php
file. It allows you to specify the connection details for different database systems such as MySQL, PostgreSQL, SQLite, and others. You can define multiple database connections and switch between them as needed.
Database Access
Laravel provides an expressive and intuitive query builder for interacting with the database. You can perform CRUD (Create, Read, Update, Delete) operations using methods and fluent chains to build queries. For example:
$users = DB::table('users')->where('active', true)->get();
$user = DB::table('users')->find(1);
DB::table('users')->where('id', 1)->update(['name' => 'John Doe']);
DB::table('users')->insert([
'name' => 'Jane Doe',
'email' => '[email protected]',
]);
In this example, the DB
facade is used to interact with the users
table. The query builder allows you to perform various operations such as retrieving all active users, finding a user by their ID, updating user data, and inserting new users.
Eloquent ORM
Laravel also provides the Eloquent ORM (Object-Relational Mapping), which allows you to work with database records as objects. Eloquent provides a simple and expressive syntax for querying and manipulating data. For example:
use App\Models\User;
$users = User::where('active', true)->get();
$user = User::find(1);
$user->name = 'John Doe';
$user->save();
$user = new User;
$user->name = 'Jane Doe';
$user->email = '[email protected]';
$user->save();
In this example, the User
model is used to interact with the users
table. Eloquent provides methods for querying, updating, and creating records, making it easy to work with database data using object-oriented syntax.
Migrations
Migrations in Laravel provide a way to manage database schema changes. They allow you to define changes to the database structure, such as creating or modifying tables, columns, indexes, and constraints, using a version-controlled approach. Migrations are written in PHP and can be easily shared with other developers. For example, let’s create a migration to create a users
table:
php artisan make:migration create_users_table --create=users
This command will generate a new migration file in the database/migrations
directory. Open the generated file and define the schema changes in the up
method:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
In this example, the up
method defines the structure of the users
table using the Schema
facade and Blueprint
class. It specifies columns such as id
, name
, email
, password
, and additional fields like email_verified_at
, rememberToken
, and timestamps.
Running Migrations:
To apply migrations and update the database schema, you can run the migrate
command in the terminal:
This command will execute any pending migrations, creating or modifying the database tables as defined in the migration files.
Rolling Back Migrations
Laravel also provides the ability to rollback migrations, reverting the last batch of executed migrations. This can be done using the migrate:rollback
command:
php artisan migrate:rollback
This command will undo the last batch of migrations, effectively rolling back the database schema changes.
By utilizing Laravel’s database features and migrations, you can easily interact with databases, perform CRUD operations, define and manage database schema changes, and leverage powerful ORM capabilities with Eloquent. Migrations provide a structured approach to database schema management, enabling collaboration and version control for your database changes.
- 2 years ago
- Zaid Bin Khalid
- 1,073 Views
-
2