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
{
}
Related
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.
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);
}
}
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
I am new in laravel. I was trying to make a custom artisan command for creating tables in my test project.I followed this link but my command was not on the artisan list.Event I tried same example given in that link but its also not worked. I dont know why its happening.
I DID THIS:
1) Run this command php artisan make:console SendEmails
2) Put complete class code in app/Console/Commands/SendEmails.php file
<?php
namespace App\Console\Commands;
use App\User;
use App\DripEmailer;
use Illuminate\Console\Command;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'email:send {user}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send drip e-mails to a user';
/**
* The drip e-mail service.
*
* #var DripEmailer
*/
protected $drip;
/**
* Create a new command instance.
*
* #param DripEmailer $drip
* #return void
*/
public function __construct(DripEmailer $drip)
{
parent::__construct();
$this->drip = $drip;
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->drip->send(User::find($this->argument('user')));
}
}
Please help me let me know what I am doing wrong.
You just forgot to Register your command.
This part: https://laravel.com/docs/5.1/artisan#registering-commands.
Open app/Console/Kernel.php and add you command class in $commands array.
like this :
protected $commands = [
Commands\SendEmails::class
];
That's it.
i set up a cronjob in cpanel, i'm using laravel 4.2. i also set to send the cronjob to my email and below yellow part is the error i received in email.
[InvalidArgumentException]
There are no commands defined in the "user" namespace.
//laravel command.php
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class CronDelFilesCommand extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'user:active';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
echo "aaa";
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
//command in cpanel
* * * * * /usr/bin/php /home/project/public_html/artisan user:active > /home/project/public_html/log.txt
what's wrong? what's the error mean?
UPDATED
thanks to #KristianHareland i use wget. :)
I think you forget to add this command to artisan app/start/artisan.php.
You shoud wright:
Artisan::add(new CronDelFilesCommand());
And don't forget about composer dump-autoload
More info you can find here.