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

zowhair's avatar

Laravel Commands (how to route to a url from command file's handle() method)?

How to route to another url with a parameter from laravel's app/console/commands/myCommand.php 's handler() method?

I want to route to another url/page/route with a parameter from handle() method of custom made command file which(route) later will call a controller and that parameter passed by handle() via route() will be handle() there in controller.

app/console/commands/myCommand.php

public function handle()
    {
        $sale_days = SaleDay::latest()->first();
        Log::info('test command file');

        $latest_date = $sale_days->date;
        // $request = Request::create('/game/end/day/'.$latest_date, 'get');
        // '<a href=' . route('game.endday', $latest_date) . '></a>';
        // return $request;
        return redirect()->to('game.endday', $latest_date);
    }
web.php

    Route::get('/game/end/day/{date}', 'SaleDayController@endday')->name('game.endday');

controller 

public function endday($date) {

    // $date = \Artisan::call('auto_day:close');
    Log::info("command endday module in saledaycontroller");
    $new_Date=Carbon::create($date);
    $prev_sale_day=$this->daySaleService->endDay($date);
    
    $new_sale_day=$this->daySaleService->firstOrCreate($new_Date->addDays(1));
    $this->daySaleService->changeCurrentDay($new_sale_day->id);
    $this->daySaleService->shiftDayGameSales($new_sale_day->id,$prev_sale_day->id);
    Log::info("command endday module in saledaycontroller ENDDDDD");

    return redirect()->route('game.sale');
}

the handle function is not working properly, How can I route from handle() method with a parameter
0 likes
1 reply
automica's avatar

If you want to get the output of your endday method, you should extract it into its own class and then you'll be able to use it both within your controller class and also your command.

<?php

namespace App\Console\Commands;

use App\Models\SaleDay;
use Illuminate\Console\Command;

class SomeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'run:command';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Execute the console command.
     *
     * @param AdditionalClass $additionalClass
     * @return int
     */
    public function handle(AdditionalClass $additionalClass): int
    {
        $sale_days = SaleDay::latest()->first();
        Log::info('test command file');

        $latest_date = $sale_days->date;

        $additionalClass->endDay($latest_date);

        return 0;
    }
}


class AdditionalClass
{
    use App\Services\DaySaleService;

    private $daySaleService;

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

    public function endDay(string $date): void
    {
        Log::info("command endday module in saledaycontroller");
        $new_Date = Carbon::create($date);
        $prev_sale_day = $this->daySaleService->endDay($date);

        $new_sale_day = $this->daySaleService->firstOrCreate($new_Date->addDays(1));
        $this->daySaleService->changeCurrentDay($new_sale_day->id);
        $this->daySaleService->shiftDayGameSales($new_sale_day->id, $prev_sale_day->id);
        Log::info("command endday module in saledaycontroller ENDDDDD");
    }
}

Arguably, as you are already created a service class, you could extract the method into there and then use the service class in both places.

Please or to participate in this conversation.