Laravel 5.5快速搭建博客api系统-1

来源:互联网 发布:淘宝发布宝贝视频 编辑:程序博客网 时间:2024/06/08 03:12

本文将讲解如何使用laravel 5.5快速搭建一个博客api系统,后续会使用这个api和angular开发一个完整的博客

安装

1.创建项目

安装Laravel installer

composer global require "laravel/installer"

安装完installer后即可在需要创建项目的目录下运行以下命令创建项目:

laravel new laravel-api

安装完成后可以运行以下命令看看是否安装成功:

php artisan serve

如果执行完后打开http://localhost:8000, 如果能看到laravel的欢迎页面则表示安装成功
如果出现以下错误提示:

RuntimeException
No application encryption key has been specified.

则在项目目录下运行:

php artisan key:generate

该命令会生成APP_KEY并写入到.env文件中,重新运行

php artisan serve

现在你应该能够看到laravel的欢迎页面了 ^_^

2.创建数据库

首先需要在.env文件中修改数据库连接参数,修改完成后的.env文件类似如下:

APP_NAME=LaravelAPP_ENV=localAPP_KEY=base64:ysuavkYsXZkOTZ5R/c1ObJELhY4puT9nesORwNXDLsw=APP_DEBUG=trueAPP_LOG_LEVEL=debugAPP_URL=http://localhostDB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=laravel-blogDB_USERNAME=rootDB_PASSWORD=BROADCAST_DRIVER=logCACHE_DRIVER=fileSESSION_DRIVER=fileQUEUE_DRIVER=syncREDIS_HOST=127.0.0.1REDIS_PASSWORD=nullREDIS_PORT=6379MAIL_DRIVER=smtpMAIL_HOST=smtp.mailtrap.ioMAIL_PORT=2525MAIL_USERNAME=nullMAIL_PASSWORD=nullMAIL_ENCRYPTION=nullPUSHER_APP_ID=PUSHER_APP_KEY=PUSHER_APP_SECRET=

我们将使用laravel提供的migration功能快速的创建我们的测试数据库
首先在本地数据库中创建一个名叫laravel-blog的数据库,表的创建工作就留给migration来完成
新建一个migration:

php artisan make:migration create_users_table

运行这条命令会创建一个migration文件,然后将这个migration文件中的内容修改如下:

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateUsersTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('users', function (Blueprint $table) {            $table->increments('user_id');  // 用户ID            $table->mediumInteger('user_group_id'); // 用户所在组的ID            $table->string('user_username', 50)->unique();  // 用户名            $table->string('user_email', 50)->unique(); // 用户邮箱地址            $table->string('user_password', 50);    // 用户密码            $table->string('user_phone', 20)->unique(); // 用户电话号码            $table->string('user_address', 300);    // 用户地址            $table->string('user_qq', 20)->unique();    // 用户qq            $table->tinyInteger('user_gender'); // 用户性别  0: 空 1: 男  2:女            $table->string('user_avatar', 150); // 用户头像图片地址            $table->dateTime('user_register_time'); // 用户注册时间            $table->ipAddress('user_register_ip');  // 用户注册IP            $table->dateTime('user_last_login_time');   // 用户上次登录时间            $table->rememberToken();    // remember_token             $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('users');    }}

同样的方法,可以创建剩下的表,各表的migration文件内容如下:

user_group表:

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateUserGroupTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('user_group', function (Blueprint $table) {            $table->increments('user_group_id');    // 用户组ID            $table->string('user_group_name', 50)->unique();    // 用户组名称            $table->string('user_group_permissions', 150);  // 用户组权限列表            $table->mediumInteger('user_group_parent_id');  // 用户组父组            $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('user_group');    }}

article表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateArticleTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('article', function (Blueprint $table) {            $table->increments('article_id');   // 文章ID            $table->mediumInteger('article_user_id');   // 发表文章用户ID            $table->string('article_title');    // 文章标题            $table->dateTime('article_post_time');  // 文章发布时间            $table->mediumInteger('article_views'); // 文章被查看次数            $table->mediumInteger('article_catalogue_id');  // 文章类别ID            $table->tinyInteger('article_is_public');   // 文章是否公开   0: 公开 1:不公开            $table->tinyInteger('article_is_top');  // 文章是否置顶   0: 不置顶 1: 置顶            $table->longText('article_content');    // 文章内容            $table->timestamps();   // // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('article');    }}

catalogue表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateCatalogueTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('catalogue', function (Blueprint $table) {            $table->increments('catalogue_id'); // 文章类别ID            $table->mediumInteger('catalogue_user_id'); // 文章类别所属用户ID            $table->string('catalogue_name');   // 文章类别名称            $table->mediumInteger('catalogue_parent_id');   // 文章类别父类别            $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('catalogue');    }}

visitor表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateVisitorTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('visitor', function (Blueprint $table) {            $table->increments('visitor_id');   // 主键            $table->mediumInteger('visitor_user_id');   // 访问者ID            $table->dateTime('visitor_visit_time'); // 访问时间            $table->ipAddress('visitor_ip');    // 访问IP            $table->mediumInteger('visitor_article_id');    // 访问文章ID            $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('visitor');    }}

comment表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateCommentTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('comment', function (Blueprint $table) {            $table->increments('comment_id');   // 评论ID            $table->mediumInteger('comment_user_id');   // 评论用户ID            $table->mediumInteger('comment_article_id');    // 被评论文章ID            $table->mediumInteger('comment_parent_id'); // 评论父评论ID            $table->ipAddress('comment_ip_address');    // 评论IP地址            $table->dateTime('comment_time');   // 评论时间            $table->longText('comment_content');    // 评论内容            $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('comment');    }}

message表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateMessageTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('message', function (Blueprint $table) {            $table->increments('message_id');   //  消息ID            $table->mediumInteger('message_from_user_id');  // 发送消息用户ID            $table->mediumInteger('message_to_user_id');    // 接受消息用户ID            $table->string('message_title', 150);   // 消息标题            $table->longText('message_content');    // 消息内容            $table->timestamps();    // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('message');    }}

concern表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateConcernTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('concern', function (Blueprint $table) {            $table->increments('concern_id');   // 关注ID            $table->mediumInteger('concern_user_id');   // 关注用户ID            $table->mediumInteger('concern_concerned_user_id'); // 被关注用户ID            $table->dateTime('concern_time'); // 关注时间            $table->timestamps();        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('concern');    }}

system_message表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateSystemMessageTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('system_message', function (Blueprint $table) {            $table->increments('system_message_id');    // 系统消息ID            $table->mediumInteger('system_message_to_user_id'); // 接受消息用户ID            $table->mediumInteger('system_message_to_group_id');    // 接受消息用户组ID            $table->tinyInteger('system_message_to_all');   // 是否发送给所有用户  0: 否  1: 是            $table->string('system_message_title', 150);    // 消息标题            $table->longText('system_message_content'); // 消息内容            $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('system_message');    }}

blog_info表

use Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateBlogInfoTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('blog_info', function (Blueprint $table) {            $table->increments('blog_info_id'); // 博客信息ID            $table->mediumInteger('blog_info_user_id'); // 博客用户ID            $table->string('blog_info_keyword', 255);   // 博客关键词            $table->string('blog_info_description', 255);   // 博客描述            $table->string('blog_info_title', 255); // 博客标题            $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('blog_info');    }}

photo_album表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreatePhotoAlbumTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('photo_album', function (Blueprint $table) {            $table->increments('photo_album_id');   // 相册ID            $table->mediumInteger('photo_album_user_id');   // 相册用户ID            $table->string('photo_album_name'); // 相册名称            $table->tinyInteger('photo_album_visit_permission');    // 相册访问限制  0: 所有人能访问 1: 关注人能访问 2: 输入密码访问 3:回答问题访问            $table->string('photo_album_password', 150);    // 相册访问密码            $table->string('photo_album_question', 255);    // 相册访问问题            $table->string('photo_album_answer', 255);  // 相册访问答案            $table->mediumInteger('photo_album_cover_image');   // 相册封面图片ID            $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('photo_album');    }}

photo表

<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreatePhotosTable extends Migration{    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('photos', function (Blueprint $table) {            $table->increments('photo_id'); // 图片ID            $table->string('photo_name', 255);  // 图片名称            $table->string('photo_address', 255);   // 链接地址            $table->string('photo_description', 255);   // 图片描述            $table->mediumInteger('photo_user_id'); // 图片所属用户ID            $table->mediumInteger('photo_album_id');    // 图片所属相册ID            $table->dateTime('photo_upload_time');  // 图片上传时间            $table->timestamps();   // created_at 和 updated_at列        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::dropIfExists('photos');    }}

创建好以上这些migration文件后,在项目根目录下运行:

php artisan migrate

运行过程中如果出现

[PDOException]  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

修改app\Providers\AppServiceProvider.php如下:

<?phpnamespace App\Providers;use Illuminate\Support\ServiceProvider;use Illuminate\Support\Facades\Schema;class AppServiceProvider extends ServiceProvider{    /**     * Bootstrap any application services.     *     * @return void     */    public function boot()    {            Schema::defaultStringLength(191);    }    /**     * Register any application services.     *     * @return void     */    public function register()    {        //    }}

运行结束后应该可以在数据库中看到生成了所有的表

原创粉丝点击