0

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.

8
  • Did you add your command to composer autoloader? Or created it manually? Commented Jan 6, 2014 at 7:52
  • I used php artisan command:make SampleCommand like in the docs. The error comes up only when I insert the use of Samplemodel. If there are no models it works fine. Commented Jan 6, 2014 at 7:57
  • Is your model namespaced App\Models? How about not writing the use line, does it help? Show your model file contents please. Commented Jan 6, 2014 at 13:05
  • Please show us your Samplemodel code. Commented Jan 6, 2014 at 13:21
  • @Vlakarados, yes my model is namespaced as App\Models. @Antonio, the actual model is pretty long (and is in use so it works just fine) which is why I made a sample which is what you see now. Let me run more tests... Commented Jan 6, 2014 at 14:23

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

3 Comments

This helped me find the answer. Simply remove the namespace as in your example and my code worked. I'm not sure if this is what you intended but after noticing the differences I simply gave it a shot.
@enchance - Your comment helped me out too. :)
I am having similar issue but your solution did not work for me. The All() method works fine but when I try to use a where() it returns nothing. I am 100% sure I am querying for the correct data. If I remove namespace I get a cannot find class when I run from command line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.