数据库迁移就像是数据库的版本控制,可以让你的团队轻松修改并共享应用程序的数据库结构
php artisan make:migration create_users_table --create=users php artisan make:migration add_votes_to_users_table --table=users //添加字段
新的迁移文件会被放置在 database/migrations 目录中。每个迁移文件的名称都包含了一个时间戳,以便让 laravel 确认迁移的顺序。
--table 和 --create 选项可用来指定数据表的名称,或是该迁移被执行时是否将创建的新数据表。
迁移类通常会包含两个方法:up 和 down。up 方法可为数据库添加新的数据表、字段或索引,而 down 方法则是 up 方法的逆操作。可以在这两个方法中使用 Laravel 数据库结构生成器来创建以及修改数据表。
/**
* 运行数据库迁移
*
* @return void
*/
public function up()
{
Schema::create('flights', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->comment('字段注解');
$table->string('airline')->comment('字段注解');
$table->timestamps();
});
}
/**
* 回滚数据库迁移
*
* @return void
*/
public function down()
{
Schema::drop('flights');
}![1531201515462877.png 3101162375-5b3ec97662302_articlex[1].png](https://img.php.cn//upload/image/140/541/937/1531201515462877.png)
数据表、字段、索引:https://laravel-china.org/doc...
运行所有未完成的迁移:php artisan migrate
回滚最后一次迁移,可以使用 rollback 命令:
php artisan migrate:rollback php artisan migrate:rollback --step=5 //回滚迁移的个数 php artisan migrate:reset //回滚应用程序中的所有迁移 php artisan migrate:refresh // 命令不仅会回滚数据库的所有迁移还会接着运行 migrate 命令 php artisan migrate //恢复
php artisan make:seeder UsersTableSeeder
/**
* 运行数据库填充
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => str_random(10),
'email' => str_random(10).'@gmail.com',
'password' => bcrypt('secret'),
]);
}![1531201529583358.png 2162984883-5b3edb51f4192_articlex[1].png](https://img.php.cn//upload/image/727/951/771/1531201529583358.png)
利用模型工厂类来批量创建测试数据
php artisan make:factory PostFactory -m Post // -m 表示绑定的model
![1531201546879580.png 52816340-5b3edfadedd55_articlex[1].png](https://img.php.cn//upload/image/911/108/357/1531201546879580.png)
![1531201553690805.png 3763343682-5b3edff3ec3fb_articlex[1].png](https://img.php.cn//upload/image/628/397/705/1531201553690805.png)
在 DatabaseSeeder 类中,你可以使用 call 方法来运行其他的 seed 类。
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call([
UsersTableSeeder::class,
PostsTableSeeder::class,
CommentsTableSeeder::class,
]);
} 默认情况下,db:seed 命令将运行 DatabaseSeeder 类,这个类可以用来调用其它 Seed 类。不过,你也可以使用 --class 选项来指定一个特定的 seeder 类:
php artisan db:seed php artisan db:seed --class=UsersTableSeeder
你也可以使用 migrate:refresh 命令来填充数据库,该命令会回滚并重新运行所有迁移。这个命令可以用来重建数据库:
php artisan migrate:refresh --seed
创建模型:
php artisan make:model Models/Goods php artisan make:model Models/Goods -m //同时生成对应的migration文件
![1531201568707903.png 2373942064-5b3eda8bcf041_articlex[1].png](https://img.php.cn//upload/image/453/997/703/1531201568707903.png)
![1531201574325741.png 1699074705-5b3f039cc5a59_articlex[1].png](https://img.php.cn//upload/image/727/459/262/1531201574325741.png)
![1531201582256820.png 4009835930-5b3f0462e0d67_articlex[1].png](https://img.php.cn//upload/image/890/570/219/1531201582256820.png)
![1531201591364519.png 178212548-5b3f1c0614fcf_articlex[1].png](https://img.php.cn//upload/image/901/240/616/1531201591364519.png)
批量创建路由:(资源路由)
php artisan make:controller UserController --resource
Route::resource('user', 'UserController'); //批量一次性定义`7`个路由根据唯一字段值来获取详情,利于SEO ![1531201601171672.png 2070143949-5b3f18756b5ff_articlex[1].png](https://img.php.cn//upload/image/929/282/858/1531201601171672.png)
Laravel 5.5 Nginx 配置:
root /example.com/public;
location / {
try_files $uri $uri/ /index.php?$query_string; }
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
![1531201612794952.png 3726427886-5b3f296bd45ed_articlex[1].png](https://img.php.cn//upload/image/779/426/531/1531201612794952.png)
php artisan make:request StoreBlogPost
![1531201618223601.png 4142705083-5b3f2d11eac0d_articlex[1].png](https://img.php.cn//upload/image/214/860/811/1531201618223601.png)
find: 通过主键返回指定的数据
$result = Student::find(1001);
get - 查询多条数据结果
DB::table("表名")->get();
DB::table("表名")->where(条件)->get();创建Model类型,方法里面声明两个受保护属性:$table(表名)和$primaryKey(主键)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model{
protected $table = 'student';
protected $primaryKey = 'id';
}以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上就是关于Laravel基础Migrations的解析的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号