job class is not accepting the argument send from controller in laravel - php

Error: Missing argument 1 for App\Jobs\ReorderDatabase::handle() is showing,
I need to pass the variable from controller and i need not to use the model,
so How I should proceed.
My controller function code is here
public function postData(Request $request)
{
$updateRecordsArray = Input::get('order');
$this->dispatch(new ReorderDatabase($updateRecordsArray));
return Response::json('Okay');
}
My job RecorderDatabase job code is
<?php namespace App\Jobs;
use App\Http\Requests\Request;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\DragDropController;
/**
* Class ReorderDatabase
* #package App\Jobs
*/
class ReorderDatabase extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* #return void
*/
public function handle($updateRecordsArray)
{
$i = 1;
foreach ($updateRecordsArray as $recordID) {
DB::table('venues')->where('id', '=', $recordID)->update(array('priority' => $i));
$i++;
}
}
}

As #lagbox mentioned, you need to pass this argument into constructor and not handle method.
Your job class should look like this:
<?php namespace App\Jobs;
use App\Http\Requests\Request;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\DragDropController;
/**
* Class ReorderDatabase
* #package App\Jobs
*/
class ReorderDatabase extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $updateRecordsArray;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($updateRecordsArray)
{
$this->updateRecordsArray = $updateRecordsArray;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$i = 1;
foreach ($this->updateRecordsArray as $recordID) {
DB::table('venues')->where('id', '=', $recordID)->update(array('priority' => $i));
$i++;
}
}
}

Related

Argument #1 ($mailable) must be of type Illuminate\Contracts\Mail\Mailable

I try to send an email to a new user. But I get this error, but I don't understand what is wrong.
TypeError: Illuminate\Mail\PendingMail::send(): Argument #1
($mailable) must be of type Illuminate\Contracts\Mail\Mailable,
App\Events\UserStored given, called in
/Users/kate_kimt/laravel/laravel_block/app/Listeners/SendMailNewUser.php
on line 31 in file
/Users/kate_kimt/laravel/laravel_block/vendor/laravel/framework/src/Illuminate/Mail/PendingMail.php
on line 122
UserStored.php:
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
class UserStored
{
public User $user;
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
SendMailNewUser.php:
namespace App\Listeners;
use App\Events\UserStored;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;
use Illuminate\Mail\Mailable;
class SendMailNewUser extends Mailable
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param \App\Events\UserStored $event
* #return void
*/
public function handle(UserStored $event)
{
Mail::to($event->user->email)->send(new UserStored($event->user));
}
}

Error with Laravel and FFMpeg in queue job

I'm using Laravel with the protonemedia/laravel-ffmpeg package https://github.com/protonemedia/laravel-ffmpeg
I create a Job to handle video conversion and concatenation and this error appears:
TypeError: Argument 1 passed to App\Jobs\VideoConversion::App\Jobs{closure}() must be an instance of FFMpeg\Filters\Video\VideoFilters, instance of FFMpeg\Filters\AdvancedMedia\ComplexFilters given in /Users/fab/Sites/add-intro/app/Jobs/VideoConversion.php:43
Here is the code of the Job:
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Video;
use Storage, Str, ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
use FFMpeg\Filters\Video\VideoFilters;
class VideoConversion implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $video;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Video $video)
{
$this->video = $video;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$filename = Str::uuid().'.mp4';
FFMpeg::fromDisk('public')
->open(['intro.mp4', $this->video->path])
->addFilter(function (VideoFilters $filters) {
$filters->resize(new \FFMpeg\Coordinate\Dimension(640, 480));
})
->export()
->toDisk('public')
->inFormat(new \FFMpeg\Format\Video\X264)
->concatWithTranscoding($hasVideo = true, $hasAudio = true)
->save($filename);
}
public function failed(Throwable $exception)
{
FFMpeg::cleanupTemporaryFiles();
dd($exception);
}
}
What I'm doing wrong ?
Thanks for help

Laravel not able to process queue model, but able to process code without queue

I'm trying to set up Events, Listeners and Jobs in one of my project. The code is able to run if I remove "implements ShouldQueue". But once I include implements ShouldQueue into the Job class, it stops working
My EventServiceProvider
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'App\Events\IncorpPaid' => [
'App\Listeners\ProcessIncorpReceived',
],
];
}
My Event Class
Namespace App\Events;
use App\Company;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class IncorpPaid
{
use Dispatchable, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
}
My listener
namespace App\Listeners;
use App\Company;
use App\Events\IncorpPaid;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Request;
use Illuminate\Queue\SerializesModels;
class ProcessIncorpReceived
{
//use SerializesModels;
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* #param IncorpPaid $event
* #return void
*/
public function handle(IncorpPaid $event)
{
\App\Jobs\SendKYC::dispatch($event->company);
}
}
And finally my problem code.
The following works. But the problem is using this doesn't queue the job.
namespace App\Jobs;
use App\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendKYC
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
/**
* Execute the job.
*
* #return void
*/
public function handle(Company $company)
{
//error_log(print_r($this->company->puc,true));
// error_log(print_r($company,true));
\App\Services\SendDocsForSignature::processKYC($this->company);
}
}
But this queues the job but it doesn't work. $this->company returns a blank company model.
namespace App\Jobs;
use App\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendKYC implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
/**
* Execute the job.
*
* #return void
*/
public function handle(Company $company)
{
\App\Services\SendDocsForSignature::processKYC($this->company);
}
}
Your last code snippet looks almost correct. You just need to declare the instance variable in which you want to store the $company value. In other words, you need to add protected $company; to your SendKYC class to be able to assign the $company value to $this->company.
You SendKYC class should then start out like this:
class SendKYC implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $company;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}

Sending Mails with Queues in Larvael

First time using queue in laravel i have a simple contact form page that user submit information and i am trying to use queue to receive that information. I am pretty sure that my setup is incorrect because when i run the queue it saying processing. My question is how come my data isn't sending and what the correct way of sending the data in the array.
AskEmailController.php
<?php
namespace App\Http\Controllers;
use App\Jobs\HelpEmailJob;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use Carbon\Carbon;
class AskEmailController extends Controller
{
public function askemail()
{
return view('help.question');
}
public function store(Request $request)
{
HelpEmailJob::dispatch()
->delay(Carbon::now()->addSeconds(5));
}
}
HelpEmailJob
namespace App\Jobs;
use App\Mail\HelpEmailMailable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class HelpEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Mail::to('mikeshasaco#gmail.com')->queue(new HelpEmailMailable());
}
}
HelpEmailMailiable
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class HelpEmailMailable extends Mailable
{
use Queueable, SerializesModels;
protected $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->$data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$data = array(
'sirname' => $this->data['sirname'],
'email' => $this->data['email'],
'bodymessage' => $this->data['bodymessage'],
);
return $this->view('help.contactinfo')
->with([
'data' =>$data,
]);
}
}
You can directly send the mail from your controller like this:
class AskEmailController extends Controller
{
public function store(Request $request)
{
Mail::to('mikeshasaco#gmail.com')->send(new HelpEmailMailable);
}
}
instead of using a job. And to make the email queue by default by implementing the ShouldQueue interface like this:
class HelpEmailMailable extends Mailable implements ShouldQueue
{
...
}
Make sure your queue is running when you test this and always restart it after you make a change in the code.

Laravel detach in event not working

I am deleting a filter which model looks like this:
namespace App;
use App\Events\FilterDelete;
use Illuminate\Database\Eloquent\Model;
class Filter extends Model
{
protected $events = [
'deleting' => FilterDelete::class
];
public function filterGroup()
{
return $this->belongsTo('App\FilterGroup');
}
public function products()
{
return $this->belongsToMany('App\Product', 'product_filters')->withPivot('id', 'quantity', 'price');
}
}
The FilterDelete looks like this:
namespace App\Events;
use App\Filter;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class FilterDelete
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $filter;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Filter $filter)
{
$this->filter = $filter;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
And the listener looks like this:
namespace App\Listeners;
use App\Events\FilterDelete;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class DeleteProductFilter
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param FilterDelete $event
* #return void
*/
public function handle(FilterDelete $event)
{
$event->filter->products()->detach();
}
}
And when I delete a filter the products are not detached and the records stay in the database. No errors are returned, evetywhing seems to be working fine. Can anyone tell me why the products are not detaching ?

Categories