简体   繁体   中英

How to call Model via artisan command in Laravel 5?

For the background, I'm a laravel 5 newbie, but I did use laravel 4 in my past projects.

In laravel 4 I just call the model straight in the artisan command but now, how do I call a model inside an artisan command. here's my current code:

<?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();
    }

}

Your issue isn't related to the command, you can use Model in commands.

Issue is in your Model, add this to the beginning of the Model:

use DB;

Note 1, Laravel models should be named with first capital: SampleModel , not sampleModel ;

Note 2, as sampleModel is excess as model already named as sampleModel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM