簡體   English   中英

如何在Laravel 5中通過artisan命令調用Model?

[英]How to call Model via artisan command in Laravel 5?

對於背景,我是一個laravel 5新手,但我在過去的項目中確實使用了laravel 4。

在laravel 4中,我只是在artisan命令中直接調用模型,但現在,如何在工匠命令中調用模型。 這是我目前的代碼:

<?php 
   namespace App\Console\Commands;
   use Illuminate\Console\Command;
   use App\Models\sampleModel as sampleModel;

   class testCommand extends Command {

  /**
  * The console command name.
  *
  * @var string
  */
   protected $name = 'test';

  /**
  * The console command description.
  *
  * @var string
  */
   protected $description = 'Command description.';

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

   /**
   * Execute the console command.
   *
   * @return mixed
   */
    public function fire()
    {
        sampleModel::sampleMethod();
    }

}

您的問題與命令無關,您可以在命令中使用Model。

問題出在您的模型中,將其添加到模型的開頭:

use DB;

注1,Laravel模型應以第一個大寫命名: SampleModel ,而不是sampleModel ;

注意2, as sampleModel超出了已經命名為sampleModel模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM