Reputation: 30511
EDIT: Added my model's namespace section.
I have a working custom Artisan command but once I start inserting a model which I created I'm immediately greeted with an error.
<?php namespace App\Command;
use App\Models\Samplemodel;
public function fire()
{
$name = $this->argument('name');
// This next line won't work
$age = Samplemodel::get_age($this->option('bday')); // Line 42
$this->line("My name is {$name} and my age is {$age}.");
}
I'm always met with the error:
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'App\\Models\\Samplemodel' not found","fi
le":"X:\\xampp\\htdocs\\laralabs\\laralabs.app\\app\\commands\\SampleCommand.php","line":42}}{"error":{"type":"Symfony\\Component\\De
bug\\Exception\\FatalErrorException","message":"Class 'App\\Models\\Samplemodel' not found","file":"X:\\xampp\\htdocs\\laralabs\\larala
bs.app\\app\\commands\\SampleCommand.php","line":42}}
I removed the other methods from this sample code to keep things clean. That's basically it, does that mean I can't use a model when creating a custom Artisan command?
As requested, the first few lines of my model:
<?php namespace App\Models;
use DB;
use Config;
use Eloquent;
use DateTime;
class Helper extends Eloquent { ... }
The actual name of my model is Helper
. This class doesn't have any properties only methods.
Upvotes: 0
Views: 4858
Reputation: 87789
It works, as long you correctly namespace your stuff. I just tested it here:
Created the command:
artisan command:make UseModel
Altered the source code to:
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class UseModel extends Command {
protected $name = 'model';
protected $description = 'Command description.';
public function __construct()
{
parent::__construct();
}
public function fire()
{
var_dump(ACR\Models\Article::all());
}
protected function getArguments()
{
return array(
);
}
protected function getOptions()
{
return array(
);
}
}
Added it to artisan.php:
Artisan::add(new UseModel);
And ran it to test:
artisan model
And it vardumped the model
This is the model:
<?php namespace ACR\Models;
class Article extends Eloquent {
protected $table = 'articles';
}
Also worked using
use ACR\Models\Article;
and
var_dump(Article::all());
Upvotes: 1