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

CodeCanyon's avatar

Laravel custom artisan command works in terminal with php artisan, but not in code although exit code is 0

In my laravel version 7 application, I have written a custom artisan command for accessing aws cli for pushing files to S3 from laravel.

I'm using AWS CLI S3 commands directly because I'm not using AWS credentials. My app is hosted on a AWS EC2 instance and IAM role has been attached to it already with full permissions to access relevant S3 bucket programatically.

Artisan command code:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class PushFiles extends Command
{
/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'S3:push {file}';

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

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return int
 */
public function handle()
{
    $file = $this->argument('file');
    exec('aws s3 cp '.$file.' s3://custom-bucket');
    echo 'aws s3 cp '.$file.' s3://custom-bucket';
}
}

And in my controller I call this command like this:

$file_path = '/storage/app/'.$file_name;
$exit_code = \Artisan::call('S3:push',['file' => $file_path]);
return $exit_code;

Both $file_name and $file_path variables are assigned relevant values.

And this returns 0 as the exit code which means successful. But file is not copying to the S3 bucket.

File is copying to S3 bucket when executing the command in the terminal as below.

php artisan S3:push <Path to file which is same as $file_path>

Furthermore, I tried this code snippet as well to see the terminal output and it shows the exact handler code that I have echo.

dd(\Artisan::output());

This is really confusing me.

0 likes
5 replies
martinbean's avatar

@codecanyon I’m really confused as to why you’re trying to execute console commands in a HTTP controller, or why you think you can’t use the AWS SDK with a role. You absolutely can.

CodeCanyon's avatar

Well, I just wanted a more simpler approach to accomplish my requirement. Therefore, I didn't try to use AWS SDK.

And I have used console command execution in my controller because it has some related to logic apart from the file pushing to S3.

martinbean's avatar

But it’s a console command. Meant to be used on the command line. Not in HTTP controllers.

You don’t call Artisan commands in controllers, and you don’t call controllers in Artisan commands.

Extract the logic to a service class and you can then call that class in both console commands and controllers.

CodeCanyon's avatar

Thanks for you suggestion.

So do you think once I moved my logic to a service class, this issue won't come? Because I doubt although I moved the logic to a service class, still the issue can be coming.

Please or to participate in this conversation.