mercredi 16 novembre 2016

Laravel Artisan Console Command Cannot Retrieve Model Collection

I am trying to do some logic via a custom Console Command I created using Artisan but I am unable to use my model class and query using Eloquent.

When I use MyModel::all() it will return all records in a collection. This is great except the model I am using has too many records to load all of them into memory.

I am trying to use

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

This returns an empty collection :(

My Console Class is as follows:

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);
        dd($websites);

        ... ( more Logic after I get website )

    }
}

My Model Class is as follows:

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 )
}    

Database Table:

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

First Dummy Data Record being processed:

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

Output on Terminal

$ 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: []
}

I have tried a handful of different ways to query including DB::select() which returns what I am looking for but I cannot dynamically set the id I am searching for with that convention.

My Model works everywhere else in my application, I think there is some sort of Namespacing issue coming into play but I am at a complete loss as to why it wouldn't work.

Any help would be very appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire