laravel入门-01

创建laravel应用

laravel new app_name

使用 PHP 内置 web server 驱动我们的网站

cd xxx/public

php -S localhost:port

查看所有可用的 Artisan 命令

php artisan list

激活某些功能 eg:auth系统

php artisan make:auth

访问auth功能

http://localhost:port/login

连接数据库

在env文件进行修改参数

数据库迁移(migration)

在应用根目录(后退一步 cd ../)

php artisan migrate

laravel5.5 迁移数据库 出错(三)

重新创建虚拟服务器,进行注册

使用 Artisan 工具新建 Model 类及其附属的 Migration 和 Seeder(数据填充)类

php artisan make:model model_name

使用 artisan 生成 Migration

php artisan make:migration create_articles_table

修改他的 up 函数

public function up()
{
    Schema::create('articles', function (Blueprint $table)
    {
        $table->increments('id');
        $table->string('title');
        $table->text('body')->nullable();
        $table->integer('user_id');
        $table->timestamps();
    });
}
这几行代码描述的是 Article 对应的数据库中那张表的结构。Laravel Model 默认的表名是这个英文单词的复数形式
创建数据表
php artisan migrate
Seeder 是我们接触到的一个新概念,字面意思为播种机。Seeder 解决的是我们在开发 web 应用的时候,需要手动向数据库中填入假数据的繁琐低效问题。
php artisan make:seeder ArticleSeeder
/database/seeds 里多了一个文件 ArticleSeeder.php,修改此文件中的 run 函数为:
public function run()
{
    DB::table('articles')->delete();

    for ($i=0; $i < 10; $i++) {
        AppArticle::create([
            'title'   => 'Title '.$i,
            'body'    => 'Body '.$i,
            'user_id' => 1,
        ]);
    }
}

上面代码中的 AppArticle 为命名空间绝对引用。如果你对命名空间还不熟悉,可以读一下 《PHP 命名空间 解惑》,很容易理解的。

接下来我们把 ArticleSeeder 注册到系统内。修改 learnlaravel5/database/seeds/DatabaseSeeder.php 中的 run 函数为:

public function run()
{
    $this->call(ArticleSeeder::class);
}

由于 database 目录没有像 app 目录那样被 composer 注册为 psr-4 自动加载,采用的是 psr-0 classmap 方式,所以我们还需要运行以下命令把 ArticleSeeder.php 加入自动加载系统,避免找不到类的错误:
composer dump-autoload
php artisan db:seed
刷新一下数据库中的 articles 表,会发现已经被插入了 10 行假数据



 
原文地址:https://www.cnblogs.com/littlebob/p/9677961.html