YasseMoh's avatar

Too few arguments to function

I use laravel 10. In my application I have a job and schedule. When I run the commands:

php artisan schedule:list php artisan schedule:run

I get this error:

Too few arguments to function App\Jobs\SendNewsletter::__construct(), 0 passed in C:\xampp\htdocs\project\app\Console\Kernel.php on line 17 and exactly 1 expected

this is my controller:

$letters=Newsletter::orderBy('id','asc')->chunk(2,  function($letters){
     $this-> dispatch(new SendNewsletter($letters));
   });

In App\Jobs\SendNewsletter

public $letters;
public function __construct($letters)
    {
      $this->letters=$letters;
    }

In app\Console|kernel.php

use App\Jobs\SendNewsletter;
protected function schedule(Schedule $schedule): void
    {
       $schedule->job(new SendNewsletter)->weekly();
    } 

Everything looks fine but it keeps throwing an error and The schedule isn't working either.

0 likes
8 replies
Snapey's avatar

What do you have in your kernel.php

And format your code with three backticks ``` before and after your code block

You can go back and edit your original question.

Snapey's avatar

So you have a job

SendNewletter which looks like;

    public $letters; 
    public function __construct($letters) 
    { 
        $this->letters=$letters;
    }

So this tells me that the job requires something passed to it.

But your kernel says;

    protected function schedule(Schedule $schedule): void 
    { 
        $schedule->job(new SendNewsletter)->weekly(); 
    }

So everything DOES NOT look fine, because as the framework says ... where is $letters that you must pass into the job?

And I not sure why you think a controller has something to do with this...?

YasseMoh's avatar

@Snapey

I use the $letters in the handle function in the job like this:

public $letters; 
    public function __construct($letters) 
    { 
        $this->letters=$letters;
    }

public function handle(): void
    {   
        $link=Linked::where('approved',1)->orderBy('id','desc')->take(3)->get();
        foreach($this->letters as $letter){
            Mail::to($letter->e_mail)->send(new MailNewsletter($link));
           
         }
    }
YasseMoh's avatar

@Snapey How should I pass it into the kernel? Like this?

protected function schedule(Schedule $schedule): void
    {
       $schedule->job(new SendNewsletter($letters))->weekly();
    }
Snapey's avatar

@YasseMoh yes of course... but you need to initialise $letters, but you should not do this in the schedule because it will do it every minute, before finding that the job only runs weekly

Why cant the job get its own $letters?

Please or to participate in this conversation.