Laravel artisan常用命令集锦

来源:互联网 发布:通过淘宝助理的模块 编辑:程序博客网 时间:2024/05/17 02:28

1、控制器

// 5.2版本创建一个空控制器php artisan make:controller BlogController// 创建Rest风格资源控制器php artisan make:controller PhotoController --resource// 指定创建位置 在app目录下创建TestControllerphp artisan make:controller App\TestController// 数据迁移php artisan migrate

2、数据迁移(Migration)

// 创建迁移php artisan make:migration create_users_table// 指定路径php artisan make:migration --path=app\providers create_users_table// 一次性创建// 下述命令会做两件事情:// 在 app 目录下创建模型类 App\Post// 创建用于创建 posts 表的迁移,该迁移文件位于 database/migrations 目录下。php artisan make:model --migration Post

3、数据填充(Seeder)

// 创建要填充的数据类php artisan make:seeder UsersTableSeeder// 数据填充(全部表)php artisan db:seed// 指定要填充的表php artisan db:seed --class=UsersTableSeeder

4、路由

// 查看所有路由php artisan route:list

5、tinker命令插入单条数据

E:\opensource\blog>php artisan tinkerPsy Shell v0.7.2 (PHP 5.6.19 鈥?cli) by Justin Hileman>>> $user = new App\User;=> App\User {#628}>>> $user->name = 'admin'=> "admin">>> $user->email = 'fation@126.com'=> "fation@126.com">>> $user->password = bcrypt('123456');=> "$2y$10$kyCuwqSpzGTTZgAPMgCDgung9miGRygyCAIKHJhylYyW9osKKc3lu">>> $user->save();"insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?)"=> true>>> exitExit:  Goodbye.

6、Request请求,主要用于表单验证

php artisan make:request TagCreateRequest

创建的类存放在 app/Http/Requests 目录下

<?php namespace App\Http\Requests;use App\Http\Requests\Request;class TagCreateRequest extends Request{    /**     * Determine if the user is authorized to make this request.     *     * @return bool     */    public function authorize()    {        return true;    }    /**     * Get the validation rules that apply to the request.     *     * @return array     */     public function rules()    {        return [            'tag' => 'required|unique:tags,tag',            'title' => 'required',            'subtitle' => 'required',            'layout' => 'required',        ];    }}

使用时只需在对应的Controller方法里引入

// 注意这里使用的是TagCreateRequestpublic function store(TagCreateRequest $request){    $tag = new Tag();    foreach (array_keys($this->fields) as $field) {        $tag->$field = $request->get($field);    }    $tag->save();    return redirect('/admin/tag')        ->withSuccess("The tag '$tag->tag' was created.");}
0 0