Getting error when run a Command

Published 2 months ago by Lalit

Getting error when run a Command

[Symfony\Component\Debug\Exception\FatalThrowableError] Type error: Argument 1 passed to App\Jobs\GenerateInvoice::failed() must be an instance of App\Jobs\Exception, instance of Symfony\Compone nt\Debug\Exception\FatalThrowableError given, called in /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedH andler.php on line 84

Best Answer (As Selected By Lalit)
kfirba

@Lalit In your createInvoice() method you have this line of code:

$data['details'] = '';

Which means you initialize the details key within the $data array to an empty string. Then somewhere else in that method, you try to interact with it as if it was an array:

$data['details'][] = $details;

And you get your error. The reason for the error is that what you are calling the shorthand array_push() on a string. Usually in PHP you can do:

// initialize an array
$array = [];
// push items to the array
$array[] = 'Item A';
$array[] = 'Item B';
// Now the array looks something like:
// -> ["Item A", "Item B"]

// The above code is a shorthand for:
$array = [];
array_push($array, 'Item A');
array_push($array, 'Item B');
// -> ["Item A", "Item B"]

What happens in your code that you try to use the $data['details'] value as if it was an array when it is actually a string (remember the $data['details'] = ''; line of code).

What you need to do is change this line of code:

$data['details'] = '';

To:

$data['details'] = [];

Also, it is better if you try to debug your code by yourself. You will learn a lot. Try going through the stack trace whenever you have an error. I know it looks long and intimidating but once you get used to it, it is a breeze to do so :) Also, don't forget the Google is any developer's best friend!

kfirba
kfirba
2 months ago (211,115 XP)

@Lalit Hey.

My guess is that you forgot to import the Exception class at the top of the GenerateInvoice class which means that every reference to the Exception class in that file will actually result in an attempt to use the App\Jobs\Exception.

The fix should be rather easy, simply add this to the use statements in your job file:

Use Exception;
Lalit

Sir Now getting this error

[Symfony\Component\Debug\Exception\FatalThrowableError] Type error: Argument 2 passed to Illuminate\Log\Writer::debug() must be of the type array, object given, called in /var/www/html/choicekid s/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 237

kfirba
kfirba
2 months ago (211,115 XP)

@Lalit I'm gonna need to see your code to help with that. Please share your job class here. Paste the code between triple backticks so it gets formatted nicely:

```

// code here

```

Lalit

Sir i am not found the probulem on php 7.0 its working but on 7.1 getting this exception

kfirba
kfirba
2 months ago (211,115 XP)

@Lalit I can't help you any further unless I see some code

Lalit

i paste code here but i thik not display to You

kfirba
kfirba
2 months ago (211,115 XP)

@Lalit I can't see it. Try pasting it again

Lalit

namespace App\Jobs;

use App\Events\InvoiceCreated; use App\Models\Enrolment; use App\Models\Invoice; use App\Models\InvoiceSystem; use App\Models\Subsidy; use Carbon\Carbon; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Eloquent\Collection; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use PDF; Use Exception;

class GenerateInvoice extends Job implements ShouldQueue { use InteractsWithQueue, SerializesModels; protected $enrolment; protected $withPDFDocument;

public function __construct(Enrolment $enrolment, bool $withPDFDocument = false)
{
    $this->enrolment       = $enrolment;
    $this->withPDFDocument = $withPDFDocument;
    //Log::info('[Generate invoice] - contructor');
    Log::debug('[Generate invoice] - contructor',[$enrolment]);
    Log::debug('[Generate invoice] - with PDF Document',[$withPDFDocument]);
}

public function handle()
{
    ini_set('max_execution_time', 0);

    Log::info('[Generate invoice] - handler');


    if (!$this->enrolment->child) {
        Log::debug('[GenerateInvoice] - handle() :: Skip generating invoice for deleted child--',
            [$this->enrolment->id]);
        return;
    }

    if ($this->enrolment->payment_starts_at > Carbon::now()) {
        Log::debug('[GenerateInvoice] - handle() :: Skip generating invoice for enrolment starting in the future',
            [$this->enrolment->payment_starts_at]);

        return;
    }



    $enrolmentCount=0;
    $enrolmentCount=Enrolment::where('id',"=",$this->enrolment->id)
                            ->whereHas('currentPeriod.subsidies',function($q){
                              $q->where('status','=','Not Lodged')
                                ->orwhere('status','=','Confirmed Lodged');
                     })->count();

    $limit = $this->enrolment->ends_at < Carbon::now() ? $this->enrolment->ends_at : Carbon::now();
    while (Invoice::where('enrolment_id', $this->enrolment->id)->where('ends_at', '>', $limit)->count() < 1 && $enrolmentCount == 0) {

        $last = Invoice::where('enrolment_id', $this->enrolment->id)->orderBy('ends_at','DESC')->first();

        //Invoices start date comes from INVOICE_START_DATE variable defined in .env file

        if(strtotime($this->enrolment->payment_starts_at) < strtotime(env('INVOICE_START_DATE'))) {
            $dt = Carbon::parse(env('INVOICE_START_DATE'));
            $invoice_start_date=$dt->startOfDay();
        }else
            $invoice_start_date = $this->enrolment->payment_starts_at->startOfDay();
      //  Log::debug('[Generate invoice] - invoice start date--',[$invoice_start_date,$this->enrolment->payment_starts_at,env('INVOICE_START_DATE')]);
      //  print_r('---'.$invoice_start_date);die;

        $invoice               = new Invoice();
        $invoice->starts_at    = $last ? $last->ends_at->addDay()->startOfDay() : $invoice_start_date;
        $invoice->enrolment_id = $this->enrolment->id;
        $invoice->createInvoice();
        //$invoice->save();

        //System generated Invoice copy
        $invoice_system               = new InvoiceSystem();
        $invoice_system->starts_at           = $invoice->starts_at;
        $invoice_system->enrolment_id        = $invoice->enrolment_id;
        $invoice_system->save();

        $enrolment_period = $this->enrolment
            ->periods()
            ->where('starts_at', '<=', $invoice->ends_at)
            ->where('ends_at', '>=', $invoice->starts_at)
            ->first();



        if ($this->withPDFDocument) {
            Log::info('[Generate invoice] - PDF document');
            $pdf = PDF
                ::loadView('admin.invoice.pdf', [
                    'invoice'          => $invoice,
                    'enrolment_period' => $enrolment_period,
                ])
                ->setOption('footer-html', view('admin.common.pdf-footer'));

            $pdf->save(storage_path($invoice->id . '.pdf'));
            Log::info('[Generate invoice] - Save document');
            $invoice->addMedia(storage_path($invoice->id . '.pdf'))->toCollectionOnDisk('invoices', 'documents');
        }

        event(
            new InvoiceCreated($invoice)
        );

    }

   
}

public function failed(Exception $exception)
{
    // handle failure
    Log::debug('[Generate Invoice] Failure - ',$exception);
}

}

kfirba
kfirba
2 months ago (211,115 XP)

@Lalit

Look at your failed method. You are trying to pass the $exception object to the debug() method when it only accepts an array.

Change this line:

Log::debug('[Generate Invoice] Failure - ',$exception);

to:

Log::debug('[Generate Invoice] Failure - ', [$exception]);

Or just pass the $exception message:

Log::debug('[Generate Invoice] Failure - ', ['error' => $exception->getMessage()]);
Lalit

@kfirba : Now getting this

[Symfony\Component\Debug\Exception\FatalThrowableError] [] operator not supported for strings

kfirba
kfirba
2 months ago (211,115 XP)

@Lalit Show me the full stacktrace of the error. I need to see on which line this error occurs

Lalit

When i use command then getting

php artisan invoices:generate

agter that getting this error

[Symfony\Component\Debug\Exception\FatalThrowableError] [] operator not supported for strings

kfirba
kfirba
2 months ago (211,115 XP)

@Lalit try running the command in verbose mode:

php artisan invoices:generate -vvv
Lalit

@

Same result [Symfony\Component\Debug\Exception\FatalThrowableError] [] operator not supported for strings

Lalit

When run command getting this

Exception trace: () at /var/www/html/choicekids/app/Models/Invoice.php:275 App\Models\Invoice->App\Models{closure}() at /var/www/html/choicekids/vendor/advmaker/carbon-period/src/CarbonPeriod.php:200 Advmaker\CarbonPeriod->each() at /var/www/html/choicekids/vendor/advmaker/carbon-period/src/CarbonPeriod.php:216 Advmaker\CarbonPeriod->eachDays() at /var/www/html/choicekids/app/Models/Invoice.php:284 App\Models\Invoice->CreateInvoice() at /var/www/html/choicekids/app/Jobs/GenerateInvoice.php:81 App\Jobs\GenerateInvoice->handle() at n/a:n/a call_user_func_array() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Container/Container.php:508 Illuminate\Container\Container->call() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:94 Illuminate\Bus\Dispatcher->Illuminate\Bus{closure}() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104 Illuminate\Pipeline\Pipeline->then() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:98 Illuminate\Bus\Dispatcher->dispatchNow() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php:47 Illuminate\Queue\CallQueuedHandler->call() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php:73 Illuminate\Queue\Jobs\Job->fire() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php:42 Illuminate\Queue\SyncQueue->push() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:184 Illuminate\Bus\Dispatcher->pushCommandToQueue() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:159 Illuminate\Bus\Dispatcher->dispatchToQueue() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:73 Illuminate\Bus\Dispatcher->dispatch() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:373 dispatch() at /var/www/html/choicekids/app/Console/Commands/GenerateInvoices.php:56 App\Console\Commands\GenerateInvoices->handle() at n/a:n/a call_user_func_array() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Container/Container.php:508 Illuminate\Container\Container->call() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Console/Command.php:169 Illuminate\Console\Command->execute() at /var/www/html/choicekids/vendor/symfony/console/Command/Command.php:261 Symfony\Component\Console\Command\Command->run() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Console/Command.php:155 Illuminate\Console\Command->run() at /var/www/html/choicekids/vendor/symfony/console/Application.php:817 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/choicekids/vendor/symfony/console/Application.php:185 Symfony\Component\Console\Application->doRun() at /var/www/html/choicekids/vendor/symfony/console/Application.php:116 Symfony\Component\Console\Application->run() at /var/www/html/choicekids/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:121 Illuminate\Foundation\Console\Kernel->handle() at /var/www/html/choicekids/artisan:35

Please sign in or create an account to participate in this conversation.