Mohammad Ohidul
Mohammad Ohidul

Reputation: 41

Laravel Artisan::queue is not running from controller

I have a artisan command signature named import:excelfile.

I want to run that command inside my controller. Everything is working fine in my controller. But the Artisan::queue('import:excelfile') is not running from controller. But it's running from CLI perfectly. Where's the problem?

Please look at my codes.

controller method code

http://pastebin.com/45dj179H

public function import(Request $request) 
{
    $excel_file = $request->file('excel_file');
    $validator = Validator::make($request->all(), [
        'excel_file' => 'required'
    ]);

    if($validator->fails()) {
        return Redirect::to('/')->withErrors($validator);
    }

    $fname = md5(rand()) . '.csv';
    $full_path = 'files';
    $excel_file->move($full_path, $fname);
    $flag_table = Flag::firstOrNew(['file_name' => $fname]);
    $flag_table->imported = 0;
    $flag_table->save();

    Artisan::queue('import:excelfile');
    
    return 'File importing is on the way';
}

artisan code

http://pastebin.com/zNCtQKZ0

protected $signature = 'import:excelfile';
protected $description = 'This imports an excel file';

public function __construct()
{
    parent::__construct();
}

public function handle()
{
    $file = Flag::where('imported', '=', '0')->orderBy('created_at', 'DESC')->first();

    $file_path = 'public/files/' . $file->file_name;

    Excel::load($file_path, function($reader) {
        $reader->each(function($sheet) {
            foreach($sheet->toArray() as $row) {
                RealState::firstOrCreate($sheet->toArray());
            }
        });
    });
}

Upvotes: 4

Views: 4064

Answers (1)

Raad
Raad

Reputation: 51

Try something along the lines of:

Artisan::queue('Some:method')->onConnection('redis')->onQueue('default')->delay(0);

Upvotes: 5

Related Questions