Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Laravel Job Property of Non Object

 

Issue:

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

 

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


    }


}

 

Solution:

The Point is that there is no Auth::user() when the Job is handled by the queue. You have to store the user id in the constructor.

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


    }


}

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon