在 Laravel 中操作 MySQL 数据库是非常直观的。Laravel 提供了多种方式来与数据库交互,包括 Eloquent ORM、查询构造器以及原始 SQL 查询。下面我将为你展示如何使用 Laravel 来执行一些基本的数据库操作。

1. 设置数据库连接

首先确保你的 .env 文件中已经正确配置了数据库连接信息。例如,对于 MySQL:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
2. 迁移和模型

Laravel 使用迁移来管理数据库表结构的变化。你可以使用 Artisan 命令来创建迁移文件和模型。

创建迁移
php artisan make:migration create_users_table --create=users
  • 1.

这个命令会在 database/migrations 目录下创建一个新的迁移文件。

编辑迁移文件

打开生成的迁移文件(例如 2024_08_13_000000_create_users_table.php),并定义表结构:

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();
    });
}

public function down()
{
    Schema::dropIfExists('users');
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
执行迁移
php artisan migrate
  • 1.
创建模型
php artisan make:model User -m
  • 1.

这将创建 User 模型文件,并生成一个新的迁移文件用于更新用户表。如果你不需要生成迁移文件,可以省略 -m 参数。

3. 使用 Eloquent ORM

Eloquent 是 Laravel 的对象关系映射(ORM)系统,它允许你以面向对象的方式与数据库交互。

创建记录
$user = new User;
$user->name = 'John Doe';
$user->email = 'john@example.com';
$user->password = bcrypt('secret');
$user->save();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
查询记录
$user = User::find(1); // 查找特定 ID 的用户
$users = User::all();  // 获取所有用户
  • 1.
  • 2.
更新记录
$user = User::find(1);
$user->name = 'Jane Doe';
$user->save();
  • 1.
  • 2.
  • 3.
删除记录
$user = User::find(1);
$user->delete();
  • 1.
  • 2.
4. 使用查询构造器

如果你更喜欢使用 SQL 语法,可以使用 Laravel 的查询构造器。

创建记录
DB::table('users')->insert([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => bcrypt('secret'),
]);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
查询记录
$users = DB::table('users')->get();
  • 1.
更新记录
DB::table('users')->where('id', 1)->update(['name' => 'Jane Doe']);
  • 1.
删除记录
DB::table('users')->where('id', 1)->delete();
  • 1.
5. 使用原始 SQL 查询

在某些情况下,你可能需要编写原始的 SQL 查询。Laravel 提供了 DB 门面来执行这样的查询。

$result = DB::select('SELECT * FROM users WHERE email = ?', ['john@example.com']);
  • 1.
6. 关联关系

Eloquent 支持多种关联关系类型,如一对一、一对多、多对多等。

一对一关系
class User extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

class Profile extends Model
{
    protected $fillable = ['bio', 'avatar'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
查询一对一关系
$user = User::with('profile')->find(1);
echo $user->profile->bio;
  • 1.
  • 2.
7. 分页

当你处理大量数据时,分页是非常有用的。

$users = User::paginate(10);
  • 1.
示例代码

下面是一个简单的示例,演示如何使用 Laravel 和 Eloquent ORM 创建和检索用户记录:

// 创建新用户
$user = new User;
$user->name = 'John Doe';
$user->email = 'john@example.com';
$user->password = bcrypt('secret');
$user->save();

// 查询所有用户
$users = User::all();

// 输出所有用户的名字
foreach ($users as $user) {
    echo $user->name . PHP_EOL;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

以上就是如何在 Laravel 中操作 MySQL 数据库的基本指南。如果你有任何具体的问题或需要进一步的帮助,请随时告诉我!