laravel 5.5 在命令中使用模型
laravel 5.5 use model in command
我是 Laravel5.5 的新手,想在我的命令中使用模型,
首先 php artisan make:model 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
然后我用谷歌搜索并找到了一些关于 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?
中添加一些配置
有人可以给我一些建议吗?想。
你已经用 use App\Model\MyTest;
导入了 class,所以你可以只写 $o = new MyTest();
.
相反,您可以删除导入语句并编写 $o = new \App\Model\MyTest();
(注意添加的反斜杠)。
我是 Laravel5.5 的新手,想在我的命令中使用模型,
首先 php artisan make:model 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
然后我用谷歌搜索并找到了一些关于 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?
中添加一些配置
有人可以给我一些建议吗?想。
你已经用 use App\Model\MyTest;
导入了 class,所以你可以只写 $o = new MyTest();
.
相反,您可以删除导入语句并编写 $o = new \App\Model\MyTest();
(注意添加的反斜杠)。