Trying to make an extension to the core db:seed command to add a couple of necessary options.
Extending the Illuminate\Database\Console\Seeds\SeedCommand
and registering my command in Kernel.php give me following output when running php artisan :
[Illuminate\Contracts\Container\BindingResolutionException]
Target [Illuminate\Database\ConnectionResolverInterface] is not instantiable while building [App\Console\Commands\TenantSeeder].
Any hints what I am missing ? the class itself below :
<?php
namespace App\Console\Commands;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Illuminate\Console\ConfirmableTrait;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
class TenantSeeder extends SeedCommand
{
use ConfirmableTrait;
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'tenant:seed';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* The connection resolver instance.
*
* #var \Illuminate\Database\ConnectionResolverInterface
*/
protected $resolver;
/**
* Create a new database seed command instance.
*
* #param \Illuminate\Database\ConnectionResolverInterface $resolver
* #return void
*/
public function __construct(Resolver $resolver)
{
parent::__construct();
$this->resolver = $resolver;
}
public function getOptions()
{
$opts = parent::getOptions();
return array_merge($opts, [
['tenant', null, InputOption::VALUE_REQUIRED, 'Tenant is required to generate tenant-specific data'],
]);
}
}
You can do it like this if you are using a regular database like MySQL:
<?php
namespace App\Console\Commands;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Illuminate\Database\DatabaseManager;
class CustomSeedCommand extends SeedCommand
{
public function __construct(DatabaseManager $databaseManager)
{
parent::__construct($databaseManager);
}
}
Related
could you help me with a problem that I have? I have created a command with php artisan make:command to generate repository type classes from existing models. The problem is that I need that instead of generating a single file from a stub, I need to generate 2 or more files. I can't find documentation regarding the subject. Currently what I have achieved is that I only generate a single file from a template.
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputArgument;
class MakeRepository extends GeneratorCommand
{
/**
* The console command name.
*
* #var string
*/
protected $name = 'make:repository';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create a new repository';
/**
* The type of class being generated.
*
* #var string
*/
protected $type = 'Repository';
/**
* #inheritDoc
*/
protected function getStub()
{
return __DIR__ . '/stubs/MakeRepository/ModelRepositoryInterface.stub';
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the model to which the repository will be generated'],
];
}
/**
* Get the default namespace for the class.
*
* #param string $rootNamespace
* #return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Repositories';
}
}
EDIT #1
I have 2 stub files inside the directory:
app/Console/Commands/stubs/MakeRepository
ModelRepository.stub
ModelRepositoryInterface.stub
I want that when you execute the command...ex: php artisan make:repository Blog, these 2 files are created in the following directory:
/app/Repositories/Blog/BlogRepository.php
/app/Repositories/Blog/BlogRepositoryInterface.php
You can write a new command to create repository interface, and then call it in MakeRepository.
I think this method is in line with SRP.
// In MakeRepository.php
// Override handle method
public function handle()
{
if (parent::handle() === false && ! $this->option('force')) {
return false;
}
if ($this->option('interface')) {
$this->call('make:repository-interface', ['name' => $this->getNameInput() . 'Interface']);
}
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the model to which the repository will be generated'],
['interface', 'i', InputOption::VALUE_NONE, 'Create a new interface for the repository'],
];
}
You can also refer to the code of make model of official.
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Foundation/Console/ModelMakeCommand.php
you can use glob to get your stubs in array then loop over them to create the multiple files
foreach(glob(stubs_path('stubs/Repository/*.stub') as $stub){
copy(stubs_path('stubs/Repository/'.$stub), $repositoryLocation . 'Repository.php');
}
I want to make a trait using php artisan command but have no idea why it is not working.
app/Console/Stubs/trait.stub
namespace App\Traits;
trait DummyTrait
{
}
app/Console/Commands/TraitMakeCommand.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use function app_path;
class TraitMakeCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'make:trait {name : Traits name you want to use.}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create a new trait';
/**
* The type of class being generated.
*
* #var string
*/
protected $type = 'Trait';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
return $this->getStub();
}
/**
* Get the stub file for the generator.
*
* #return string
*/
protected function getStub()
{
return app_path('Console/Stubs/trait.stub');
}
/**
* Get the default namespace for the class.
*
* #param string $rootNamespace
*
* #return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Traits';
}
}
app/Console/Kernel.php
class Kernel extends ConsoleKernel
{
...
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\TraitMakeCommand::class,
];
...
}
Artisan output for: pa make:trait -h
Description:
Create a new trait
Usage:
make:trait <name>
Arguments:
name Traits name you want to use.
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
You can use the GeneratorCommand to generate files for stubs. This base class implements most of the logic. All the custom settings can be achieved by overriding some methods. Checkout the example below:
app/Console/Commands/TraitMakeCommand.php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand;
class TraitMakeCommand extends GeneratorCommand
{
protected $name = 'make:trait';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create a new trait';
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'make:trait {name : Traits name you want to use.}';
/**
* The type of class being generated.
*
* #var string
*/
protected $type = 'Trait';
/**
* Get the stub file for the generator.
*
* #return string
*/
protected function getStub()
{
return app_path('Console/Stubs/trait.stub');
}
/**
* Get the default namespace for the class.
*
* #param string $rootNamespace
*
* #return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace . '\Traits';
}
}
and update the stub:
app\Console\Stubs\trait.stub
<?php
namespace App\Traits;
trait DummyClass
{
}
and then the usage would be:
php artisan make:trait Example
which would result in the file: app/Traits/Example.php with the following content:
<?php
namespace App\Traits;
trait Example
{
}
I'm trying to send an email using a command in Laravel. I would like to send a file from a specific folder. Previously I did it using a view with a form, but now I want to send the email using a command. The file will always be in the same folder.
This is the command code:
<?php
namespace efsystem\Console\Commands;
use Illuminate\Console\Command;
use Storage;
use Mail;
use Config;
class SendEmailEfsystem extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'emails:send';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Sending emails to the users';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$data = array(
'destino' => 'example#gmail.com',
'asunto' => 'example',
);
Mail::send('administracion.email.email_body', $data, function ($message) use ($data) {
$message->to($data['destino']);
$message->subject($data['asunto']);
$message->from(Config::get('mail.username'));
});
$this->info('The emails are send successfully!');
}
}
Since in the "form" you used $request['a_file'] the variable was an instance of Symfony\Component\HttpFoundation\File\UploadedFile wich is an extention of Symfony\Component\HttpFoundation\File\File.
what you need to do is instantiate a File class with the path you have.
$data = array(
'destino' => 'example#gmail.com',
'asunto' => 'example',
'a_file' => new \Symfony\Component\HttpFoundation\File\File($pathToFile, true)
);
You can use N69S a_file answer
This is basic tips to help you run your command.
Your Kernel.php must be like this
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\SendEmailEfsystem::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:send')->everyMonth();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
Then if you want to run it. Simply run php artisan emails:send
or You want to run it using code you can use Artisan::call('emails:send);
i have created following command in laravel using command php artisan make:command RedisDataListener
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RedisDataListener extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'listen:subscribe';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Subscribe to redis channel for listening events';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
$value = cache()->has('tracking_application:mykey') ? cache()->get('tracking_application:mykey') : 'no';
print_r(array(
$value
));
}
}
but when i'm using cache()->get('key') it is not working. Same function works fine when i use in controller.
i'm using redis as the cache server.
also tried with cache::get() but nothing happen.
I am creating a console (Artisan) command to run a custom-made package. The import of the package and all of its functions is working just fine, but I can't seem to query any eloquent models without having the following error pop up:
[Symfony\Component\Debug\Exception\FatalErrorException]
Interface 'Illuminate\Contracts\Queue\QueueableCollection' not found
Here is my code...
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Property;
use Sync;
class SyncTool extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'sync:all';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$credentials = [
...
];
$sync = new Sync( $credentials );
$properties = Property::all(); // this throws the error
}
}
Step 1 : Remove full vendor folder
Step 2: delete /bootstrap/cache/services.php, /bootstrap/cache/compiled.php
Step 3 : Run from your terminal composer install
try this