Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ThinkingMan's avatar

Laravel Command Debugging

I have an app that is supposed to post orders to a 3rd part delivery provider and it was working at one point but now something has changed so I am trying to trace the point of failure.

There is a scheduled command to post these orders every 10 minutes. I have checked the schedule and it seems to be running just fine. I can manually run as well and I get the response that the command has processed.

The code below shows that it is supposed to log the success or failure but I am not getting anything in the log so I suspect it's something within this code. (using https://github.com/melihovv/laravel-log-viewer)

I have created a button and route to run this manually on the UI and I get a UI message that it ran successfully but I am sure it is a false positive. So I guess I need to know how to test this from the terminal and then dd the detail. Or if there is something obvious here then that would be helpful.

Thanks in advance!


namespace App\Console\Commands;

use App\Models\OrderHeader;
use App\Services\ApiRequest;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class getswiftDeliveriesUpload extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync:getswift-deliveries';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'syncing all the customers order deliveries into getswift ';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $users = User::all();

        foreach ($users as $user)
        {
            Auth::loginUsingId($user->id);

            $getswift_key = auth()->user()->getMerchantGetswiftKey();

            if (is_null($getswift_key))
            {
                Log::info("getswift order sync: this merchant ".auth()->user()->email. " not have the getswift key");
                continue;
            }

            $order_headers = OrderHeader::query()->where(function ($q){
                $q->whereNull('getswift_status')->orWhere('getswift_status' , OrderHeader::DELIVERY_NEW);
            })->orderByDesc('id')->get();

            foreach ($order_headers as $order_header)
            {
                if (Carbon::parse($order_header->creation_date)->lessThan(Carbon::now()->addMonth()))
                    continue;

                $url = config('getswift.base_url').config('getswift.deliveries');

                $request_data = $this->mappingGetswiftFeilds($order_header , $getswift_key);

                list($response , $httpcode) = ApiRequest::curlRequest($url , $request_data);

                echo "response code ". $httpcode ." ";
                if ($httpcode != 200 && isset($response->message))
                {
                    Log::error('getswift order sync: order_number: '.$order_header->order_number.' -----message:---- '.$response->message);
                    continue;
                }

                $order_header->getswift_status = OrderHeader::DELIVERY_ADDED;

                if ($order_header->save())
                {
                    Log::info('getswift order sync: order_number: '.$order_header->order_number.' -----message:---- : posted successfully');
                }

            }

            Auth::logout();
        }

    }

    private function mappingGetswiftFeilds($order_header , $getswift_key)
    {
        $order_store_info = $order_header->storeInfo;

        $items = [];
        foreach ($order_header->orderDetails as $orderDetail)
        {
            $item = [];
            $item['quantity'] =  $orderDetail->qty_selling_units;
            $item['sku'] =  trim($orderDetail->sku_number);
            $item['description'] =  $orderDetail->description;
            $item['price'] =  $orderDetail->cust_price;
            $items[] = $item;
        }

        $data = array (
            'apiKey' => $getswift_key,
            'booking' =>
                array (
                    'reference' => $order_header->order_number,
                    'deliveryInstructions' => $order_header->delivery_instruction, // need to add this deliveryInstruction in orderDetail model in orderheader table
                    'items' => $items,
                    'pickupTime' => \Carbon\Carbon::parse($order_header->creation_date)->format('Y-m-d\Th:m:s.000000+00:00'),
                    'pickupDetail' =>
                        array (
                            'name' => $order_store_info->long_name,
                            'phone' => $order_store_info->number,
                            'email' => auth()->user()->email,
                            'description' => $order_store_info->long_name,
                            'address' => $order_store_info->full_address,

                        ),
                    'dropoffWindow' =>
                        array (
                            'earliestTime' => \Carbon\Carbon::parse($order_header->delivery_date)->format('Y-m-d\Th:m:s.000000+00:00'),
                            'latestTime' => \Carbon\Carbon::parse($order_header->delivery_date)->format('Y-m-d\Th:m:s.000000+00:00'),
                        ),
                    'dropoffDetail' =>
                        array (
                            'name' => $order_header->ship_to_name,
                            'phone' => $order_header->phone_no,
                            'email' => $order_header->ship_to_email_address,
//                            'description' => '',
                            'address' => $order_header->full_address,
                        ),
                    'webhooks' =>
                        array(
                            [
                                'eventName' => 'job/added',
                                'url' => route('delivery.job.added', $order_header->id)
                            ],
                            [
                                'eventName' => 'job/accepted',
                                'url' => route('delivery.job.accepted', $order_header->id)
                            ],
                            [
                                'eventName' => 'job/driveratpickup',
                                'url' => route('delivery.job.driverpickup', $order_header->id)
                            ],
                            [
                                'eventName' => 'job/onway',
                                'url' => route('delivery.job.onway', $order_header->id)
                            ],
                            [
                                'eventName' => 'job/driveratdropoff',
                                'url' => route('delivery.job.driveratdropoff', $order_header->id)
                            ],
                            [
                                'eventName' => 'job/finished',
                                'url' => route('delivery.job.finished', $order_header->id)
                            ],
                            [
                                'eventName' => 'job/cancelled',
                                'url' => route('delivery.job.cancelled', $order_header->id)
                            ],

                        )
                        ),
                    );

        return $data;
    }
}

0 likes
3 replies
bobbybouwmann's avatar

I see a lot of code and basically it can go wrong in a lot places. So if a case goes wrong and you have a check for that in your code you should see that in the logs. However sometimes an exception occurs within the code. If this is the case you might want to use a try catch and log the exception.

To actually figure out whats going wrong it's important to reproduce this locally. You can keep trying until you have a case or you can write a test for this with some test data and see wherever this goes wrong ;)

ThinkingMan's avatar

@BOBBYBOUWMANN - Thanks and yeah, I am tracing it now but was trying to find the best way to trap the error. So you think adding more try/catch will help.

bobbybouwmann's avatar

Yeah adding more try catch will help you at least catch the error but also where it went wrong ;)

Please or to participate in this conversation.