2017-11-07 13 views
0

私はLaravel5.5の初心者で、私のコマンドでモデルを使用したいのですが、
最初のphp artisan make:model MyTest、次に私はmodelを使ってmysqlからデータを取得します。
コマンドファイル:laravel 5.5コマンドの使用モデル

<?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出力は次のとおりです:

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

とモデルファイルがある

[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にいくつかの設定を追加する必要がありますか?
誰かから助言をいただけますか?思う。

答えて

0

すでにuse App\Model\MyTest;のクラスをインポートしているので、$o = new MyTest();と書くことができます。
代わりに、インポートステートメントを削除して$o = new \App\Model\MyTest();(追加されたバックスラッシュに注意してください)を書くことができます。

+0

ああ、私は理解していないので、**名前空間**を理解してから、** PHP名前空間**の内容をもう一度読むようになります。 –