2017-10-06 86 views
0

我有一個簡單的工作設置的redis隊列。工作代碼如下。我不斷收到以下錯誤 -非對象Laravel作業屬性

ErrorException: Trying to get property of non-object in /app/Jobs/ResolveOrders.php:42 

如果我把這個相同的代碼,並將其移動到我的控制器它的作品。但是,當我在這裏移動時,它甚至不運行。這裏是我的ResolveOrders.php - 好像Laravel是不承認我的同步DB模式

<?php 

namespace App\Jobs; 

use Illuminate\Support\Facades\Auth; 
use App\Sync; 
use App\Helpers\getOrdersHelpers; 
use Carbon\Carbon; 
use Illuminate\Bus\Queueable; 
use Illuminate\Queue\SerializesModels; 
use Illuminate\Queue\InteractsWithQueue; 
use Illuminate\Contracts\Queue\ShouldQueue; 
use Illuminate\Foundation\Bus\Dispatchable; 



class ResolveOrders implements ShouldQueue 
{ 
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; 

    public $tries = 1; 

    public $timeout = 660; 

    /** 
    * Create a new job instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 

    } 

    /** 
    * Execute the job. 
    * 
    * @return void 
    */ 
    public function handle() 
    { 
     $job = Sync::create(['user_id' => Auth::user()->id, 
        'last_updated_date' => Carbon::now(), 
        'status' => 'running']); 

     $getOrdersHelpers = new getOrdersHelpers(); 
     $return = $getOrdersHelpers->getOrders(1, $job); 
     $pageNum = $return[0]; 
     $totalPages = $return[1]; 

     if ($totalPages > $pageNum) { 
      while ($pageNum <= $totalPages) { 
       $getOrdersHelpers->getOrders($pageNum, $job); 
       $pageNum++; 
      } 
     } 

      $job->status = "complete"; 
     $job->save(); 


    } 


} 

回答

1

問題是,有沒有驗證::用戶()當作業由隊列處理。您必須將用戶標識存儲在構造函數中。

<?php 

namespace App\Jobs; 

use Illuminate\Support\Facades\Auth; 
use App\Sync; 
use App\Helpers\getOrdersHelpers; 
use Carbon\Carbon; 
use Illuminate\Bus\Queueable; 
use Illuminate\Queue\SerializesModels; 
use Illuminate\Queue\InteractsWithQueue; 
use Illuminate\Contracts\Queue\ShouldQueue; 
use Illuminate\Foundation\Bus\Dispatchable; 



class ResolveOrders implements ShouldQueue 
{ 
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; 

    public $tries = 1; 

    public $timeout = 660; 

    protected $user_id; 

    /** 
    * Create a new job instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->user_id = Auth::user()->id; 
    } 

    /** 
    * Execute the job. 
    * 
    * @return void 
    */ 
    public function handle() 
    { 
     $job = Sync::create(['user_id' => $this->user_id, 
        'last_updated_date' => Carbon::now(), 
        'status' => 'running']); 

     $getOrdersHelpers = new getOrdersHelpers(); 
     $return = $getOrdersHelpers->getOrders(1, $job); 
     $pageNum = $return[0]; 
     $totalPages = $return[1]; 

     if ($totalPages > $pageNum) { 
      while ($pageNum <= $totalPages) { 
       $getOrdersHelpers->getOrders($pageNum, $job); 
       $pageNum++; 
      } 
     } 

      $job->status = "complete"; 
     $job->save(); 


    } 


} 
相關問題