Laravel artisan常用命令集锦

1、控制器 or Model

// 5.2版本创建一个空控制器
php artisan make:controller BlogController
// 创建Rest风格资源控制器
php artisan make:controller PhotoController --resource
// 指定创建位置 在app目录下创建TestController
php artisan make:controller App\TestController
// 指定路径创建
php artisan make:Model App\\Models\\User(linux or macOs 加上转义符)
// 数据迁移
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 tinker
Psy 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`) v
alues (?, ?, ?, ?, ?)"
=> true
>>> exit
Exit:  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方法里引入

// 注意这里使用的是TagCreateRequest
public 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.");
}

7、 创建artisan命令行(laravel5.*版本)

// 以下命令生成文件 app/Console/Commands/TopicMakeExcerptCommand.php

php artisan make:console TopicMakeExcerptCommand --command=topics:excerpt
//在 app/Console/Kernel.php 文件里面, 添加以下
protected $commands = [
        \App\Console\Commands\TopicMakeExcerptCommand::class,
    ];
//激活artisan命令行。
//在生成的TopicMakeExcerptCommand.php 文件, 修改以下区域
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class TopicMakeExcerptCommand extends Command
{
    /**
     * 1. 这里是命令行调用的名字, 如这里的: `topics:excerpt`, 
     * 命令行调用的时候就是 `php artisan topics:excerpt`
     *
     * @var string
     */
    protected $signature = 'topics:excerpt';

    /**
     * 2. 这里填写命令行的描述, 当执行 `php artisan` 时
     *   可以看得见.
     *
     * @var string
     */
    protected $description = '这里修改为命令行的描述';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 3. 这里是放要执行的代码, 如在我这个例子里面,
     *   生成摘要, 并保持.
     *
     * @return mixed
     */
    public function handle()
    {
        $topics = Topic::all();
        $transfer_count = 0;

        foreach ($topics as $topic) {
          if (empty($topic->excerpt))
          {
              $topic->excerpt = Topic::makeExcerpt($topic->body);
              $topic->save();
              $transfer_count++;
          }
        }
        $this->info("Transfer old data count: " . $transfer_count);
        $this->info("It's Done, have a good day.");
    }
}
// 命令行调用
php artisan topics:excerpt 
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值