How do I call a model via the artisan command in Laravel 5?

For background, I'm a laravel 5 newbie, but I've used laravel 4 in my past projects.

In laravel 4 I just call the model directly in the artisan command, but now how can I name the model inside the 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();
    }

}

      

+3


source to share


1 answer


Your problem is not command related, you can use Model command in commands.

The problem is in your model, add it to the beginning of the model:

use DB;

      




Note 1, Laravel models must be named with first capital:, SampleModel

not SampleModel

;

Note 2 as sampleModel

is redundant as the model already named as SampleModel

.

+2


source







All Articles