laravel常用命令

2019-12-11  本文已影响0人  Continue_li
composer create-project laravel/laravel learnlaravel5 ^5.5`
php artisan make:auth
php artisan migrate
php artisan make:model Article
php artisan make:migration create_articles_table
php artisan make:seeder UsersTableSeeder

function 前置用scope

laravel-admin

  1. 建立模型,并创建 Migrations:
php artisan make:model Movie -m
  1. 在 Migrations,增加一个字段:name
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 50)->unique();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('movies');
    }
}
  1. 运行 Migrations,创建对应数据库:
php artisan migrate
  1. 有了数据表,就需要往表里插入 fake 数据,用于测试
    // 使用该插件创建 fake 数据
composer require fzaninotto/faker
  1. 建立 Seeder
php artisan make:seeder MovieTableSeeder
<?php

use Illuminate\Database\Seeder;

class MovieTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        $faker = Faker\Factory::create();

        for($i = 0; $i < 1000; $i++) {
            App\Movie::create([
                'name' => $faker->name
            ]);
        }
    }
}
php artisan db:seed --class=MovieTableSeeder

是不是很简单,数据表直接填充 1000 条假数据:>

  1. 建立资源 Controller
php artisan admin:make MovieController --model=App\Movie

这样就直接有了基础的增删改查和 movie 列表功能的 Controller 了。>

  1. 建立 route
$router->resource('movies', MovieController::class);
  1. 加入到 admin 的 menu 中

其中路径处需要注意的是:
其中uri填写不包含路由前缀的的路径部分,比如完整路径是http://localhost:8000/admin/demo/users, 那么就填demo/users,如果要添加外部链接,只要填写完整的url即可,比如http://laravel-admin.org/.
上图也是加了左侧 movies 菜单的效果。
这就完成了简单的 movie 资源的后台管理了,在浏览器输入链接:
http://web.app/admin/movies
就能看到一个较为完整的 movie 列表:

总结
php artisan make:model Movie -m

php artisan migrate

composer require fzaninotto/faker

php artisan make:seeder MovieTableSeeder

php artisan db:seed --class=MovieTableSeeder

php artisan admin:make MovieController --model=App\\Movie

$router->resource('movies', MovieController::class);
上一篇下一篇

猜你喜欢

热点阅读