John Davidson

php - Laravel7.x 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.


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);


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


public function endday($date) {

// $date = \Artisan::call('auto_day:close');
Log::info("command endday module in saledaycontroller");

Log::info("command endday module in saledaycontroller ENDDDDD");

return redirect()->route('');

the handle function is not working properly, How can I route from handle() method with a parameter

My Answer:
To route to a URL from a command file in Laravel 7.x, you can use the `route()` helper function provided by Laravel. Here's an example of how you can use this function in a command file:


namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyCommand extends Command
protected $signature = 'mycommand:route';

protected $description = 'Route to a URL from a command file';

public function __construct()

public function handle()
$url = route(''); // Replace '' with the name of the route you want to route to
$this->info('Routing to URL: ' . $url);

In the above example, we are using the `route()` helper function to generate the URL for a named route. Make sure to replace `''` with the actual name of the route you want to route to.

To run this command, you can use the following command in your terminal:

php artisan mycommand:route

This will output the URL that the command is routing to.

Rate this post

3 of 5 based on 8323 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us