Kugutsumen
Kugutsumen

Reputation: 600

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

}

Upvotes: 3

Views: 1930

Answers (1)

Limon Monte
Limon Monte

Reputation: 54399

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.

Upvotes: 2

Related Questions