2016-11-16 15 views
0

Artisanを使用して作成したカスタムコンソールコマンドでいくつかのロジックを実行しようとしていますが、Eloquentを使用してモデルクラスとクエリを使用できません。Laravel Artisanコンソールコマンドでモデルコレクションを取得できません

MyModel :: all()を使用すると、コレクション内のすべてのレコードが返されます。これは、私が使っているモデルがあまりにも多くのレコードを持っていて、それらのすべてをメモリに読み込むことを除けば、素晴らしいことです。

私はこれは空のコレクションを返します:(以下のように

私のコンソールクラスです

MyModel::where('id',$currentLoopId)->get(); 

を使用しようとしています:以下のように

namespace App\Console\Commands; 

use Illuminate\Console\Command; 
use App\ProcessReferral; 
use App\TargetUrl; 
use App\Website; 

class ProcessReferrals extends Command 
{ 
    /** 
    * The name and signature of the console command. 
    * 
    * @var string 
    */ 
    protected $signature = 'process:referrals'; 

    /** 
    * The console command description. 
    * 
    * @var string 
    */ 
    protected $description = 'Process all outstanding records in process_referrals table'; 

    protected $logArray = []; 
    /** 
    * Create a new command instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
    parent::__construct(); 
    } 

    /** 
    * Execute the console command. 
    * 
    * @return mixed 
    */ 
    public function handle() 
    { 
    $this->info('Start Processing Referrals'); 

    $process_referrals = ProcessReferral::all();   
    $referral_count = count($process_referrals); 

    $this->info('Found '.$referral_count.' referral(s) that need processed'); 


    foreach($process_referrals as $referral){ 

     ## 1. SEE IF REFERRAL WEBSITE IS ACTIVE AND VERIFIED IN AFFILIATE_WEBSITES. LOAD LAST FORCE UPDATE REFRESH CODES 
     ######################################## 

     $this->info('Processing Referral ID: '.$referral->id); 
     $this->info('Get Website from Affiliate Website Id: '.$referral->affiliate_website_id); 

     $websites = Website::where('id','=',$referral->affiliate_website_id)->get(); 
     dd($websites); 

     ... (more Logic after I get website) 

    } 
} 

私のモデルクラスは次のとおりです。

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Website extends Model 
{ 

    /** 
    * The table associated with the model. 
    * 
    * @var string 
    */ 
    protected $table = 'affiliate_websites'; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     ... 
    ]; 

    ... (Eloquent Relations) 
}  

D表atabase:

id website_label referrals 
------------------------------ 
1 Google UK  920685 
2 Google U.S.  2940884 
3 Google Germany 709603 

第1のダミーデータレコードが処理されている:

id affiliate_website_id 
------------------------- 
2 3 

出力端子

$ php artisan process:referrals 
Start Processing Referrals 
Found 300 referral(s) that need processed 
Processing Referral ID: 2 
Get Website from Affiliate Website Id: 3 
Website(id->62) is Active. 
Illuminate\Database\Eloquent\Collection {#901 
    #items: [] 
} 

上の私は、DBを含むクエリにさまざまな方法のほんの一握りを試してみました::選択します( )私が探しているものを返すが、私はその慣習で探しているIDを動的に設定することはできません。

私のモデルはアプリケーションのどこにでも動作しますが、何らかの名前空間の問題が出てきていると思いますが、なぜうまくいかないのか全くわかりません。

ご協力いただければ幸いです。 >最初の()を取得する -

+0

上記のコードでは、 'ウェブサイト:: where()'はtypoですか? –

+0

はいそれはタイプミスでした – Scottzozer

答えて

0

としては、私は私が渡したIDが

$website = Website::where('id',(int)$referral->affiliate_website_id)->first(); 

は、まだいくつかの問題があったが、使用される整数だったかどうかを確認するためにチェックしhttp://laravel.io/chat

に@machugaによって指摘しました単一のレコードを - > get()ではなくウェブサイトのコレクションをループする必要はありません

関連する問題