laravel 5.5使用模型命令

时间:2017-11-07 10:16:57

标签: php laravel-5.5

我是Laravel5.5的新手,想在我的命令中使用模型,
第一个php artisan make:模型MyTest,然后我用模型从mysql中获取数据 命令文件:

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Model\MyTest;

class UboxDataAnalysis extends Command
{
    //...

    public function handle()
    {
        $this->line('Then start query');
        $o = new App\Model\MyTest();
    }
}

模型文件是:

<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use DB;

class MyTest extends Model
{
    //
    function get_data(){
        $uboxTest = DB::connection('mysql-test');
        $res = $uboxTest::table('m_user')->where('user_id',166);
        var_dump($res);
    }
}

但CLI输出是:

[Symfony\Component\Debug\Exception\FatalThrowableError]    
Class 'App\Console\Commands\App\Model\MyTest' not found

我用Google搜索并找到关于laravel5.5 doc的内容 To get started, let's create an Eloquent model. Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file.
那就是说,我需要在composer.json中添加一些配置吗? 有人可以给我一些建议吗?认为

1 个答案:

答案 0 :(得分:0)

您已使用use App\Model\MyTest;导入了该类,因此您只需撰写$o = new MyTest(); 而不是你可以删除import语句并写$o = new \App\Model\MyTest();(注意添加的反斜杠)。

相关问题