paranoid
paranoid

Reputation: 7115

Run artisan command in laravel 5

I have controller like this

 public function store(Request $request)
{
   Artisan::call("php artisan infyom:scaffold {$request['name']} --fieldsFile=public/Product.json");
}

Show me error

There are no commands defined in the "php artisan infyom" namespace.

When I run this command in CMD it work correctly

Upvotes: 59

Views: 126579

Answers (6)

Mahdi Bashirpour
Mahdi Bashirpour

Reputation: 18873

Apart from within another command, I am not really sure I can think of a good reason to do this. But if you really want to call a Laravel command from a controller (or model, etc.) then you can use Artisan::call()

Artisan::call('email:send', [
        'user'    => 1,
        '--queue' => 'default'
]);

One interesting feature that I wasn't aware of until I just Googled this to get the right syntax is Artisan::queue(), which will process the command in the background (by your queue workers):

Route::get('/foo', function () {
    Artisan::queue('email:send', [
        'user'    => 1,
        '--queue' => 'default'
    ]);
    //
});

If you are calling a command from within another command you don't have to use the Artisan::call method - you can just do something like this:

public function handle()
{
    $this->call('email:send', [
        'user'    => 1,
        '--queue' => 'default'
    ]);
    //
}

Source: https://webdevetc.com/programming-tricks/laravel/general-laravel/how-to-run-an-artisan-command-from-a-controller/

Upvotes: 5

Unicco
Unicco

Reputation: 2578

Method #1: Using route

Route::get('run-it', function () {
    (new \App\Console\Commands\ThisIsMyCommand())->handle();
});

Method #2: Using command line

php artisan command:this_is_my_command

Upvotes: 0

Muhammad Zubair
Muhammad Zubair

Reputation: 21

Remove php artisan part and try:

Route::get('/run', function () {
    Artisan::call("migrate");
});

Upvotes: 2

Thushara Buddhika
Thushara Buddhika

Reputation: 1820

Command Job,

Path : {project-path}/app/Console/Commands/RangeDatePaymentsConsoleCommand.php

This is the Job that runs with the artisan command.

class RangeDatePaymentsConsoleCommand extends Command {
    protected $signature = 'batch:abc {startDate} {endDate}';
    ...
}

web.php,

Path : {project-path}/routes/web.php

web.php manage all the requests and route to relevant Controller and can have multiple routes for multiple controllers and multiple functions within the same controller.

$router->group(['prefix' => 'command'], function () use ($router) {
    Route::get('abc/start/{startDate}/end/{endDate}', 'CommandController@abc');
});

CommandController.php,

Path : {project-path}/app/Http/Controllers/CommandController.php

This Controller is created for handle artisan commands and name can be vary but should be same to web.php Controller name and the function name.

class CommandController extends Controller {

    public function abc(string $startDate, string $endDate) {
        $startDate = urldecode($startDate);
        $endDate = urldecode($endDate);

        $exitCode = Artisan::call('batch:abc',
            [
                'startDate' => $startDate,
                'endDate' => $endDate
            ]
        );
        return 'Command Completed Successfully. ';
    }

Request : http://127.0.0.1:8000/command/abc/start/2020-01-01 00:00:00/end/2020-06-30 23:59:59

It's can be access through the web browser or Postman after startup the server. Run this command to start the php server at {project-path}

php -S 127.0.0.1:8080 public/index.php

Upvotes: 0

Nole
Nole

Reputation: 847

If you have simple job to do you can do it from route file. For example you want to clear cache. In terminal it would be php artisan cache:clear In route file that would be:

Route::get('clear_cache', function () {

    \Artisan::call('cache:clear');

    dd("Cache is cleared");

});

To run this command from browser just go to your's project route and to clear_cache. Example:

http://project_route/clear_cache

Upvotes: 24

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

You need to remove php artisan part and put parameters into an array to make it work:

public function store(Request $request)
{
   Artisan::call("infyom:scaffold", ['name' => $request['name'], '--fieldsFile' => 'public/Product.json']);
}

https://laravel.com/docs/5.2/artisan#calling-commands-via-code

Upvotes: 85

Related Questions