Laravel: how to simplify artisan commands? - laravel

I want to simplify the following artisan commands because I have several databases and the migrations for each database are stored in a separate folder.
php artisan make:migration {{name}} --path=/database/migrations/{{folder}}
php artisan migrate:rollback --path=/database/migrations/{{folder}}
to
php artisan make:migration {{name}} {{folder}}
php artisan migrate:rollback {{folder}}
Is this possible and if so how can I implement it?

Since this is not an option in the Laravel commands, a way to implement this yourself is by writing you own commands that call other artisan commands.
To do so, in your terminal write, for example, php artisan make:command MigrateWithPath to create a new command at app/Console/Commands/MigrateWithPath.php. Then, you can call the vanilla implementation Laravel provides at (vendor\laravel\framework\src) \Illuminate\Database\Console\Migrations\MigrateMakeCommand but then in a way that you specify.
Be sure though that the name of your new command needs to be different from the Laravel one, to prevent recursions. Therefore, I have prefixed the name with app: to be like app:make:migration, but feel free to use something else.
Take a look at the following suggestion:
class MigrateWithPath extends BaseCommand
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'app:make:migration {name : The name of the migration}
{folder? : The location where the migration file should be created}';
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$this->call('make:migration', [
'name' => $this->argument('name'),
'--path' => '/database/migrations/' . $this->argument('folder'),
]);
return 0;
}
Then do the same for the rollback command.

Related

Laravel: How to add signature option to migrate:rollback command?

I have multiple databases and the migrations for each database are stored in a different folder. Hence, I want to override the migrate:rollback command and add an option for a folder instead of specifying the path every time.
So instead of running the following command:
php artisan migrate:rollback --path=/database/migrations/{{folder}}
I want to run:
php artisan migrate:rollback {{folder}}
How can I achieve this?
protected $signature = 'test {folder}';
protected $description = 'Testing Purpose';
public function handle()
{
$this->ask('Enter Folder Name?');
$folder = $this->argument('folder');
$command = "migrate:rollback --path=/database/migrations/{$folder}";
Artisan::call($command);
}

Custom Laravel Artisan Make Controller command

I want to put my controller generated by the artisan command to a custom directory. I made own command
php artisan make:command ApiControllerMake
and extended it
class ApiControllerMake extends ControllerMakeCommand
then I removed everything from there and overridden method
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Http\AppAPI\Controllers';
}
It's working OK.
Then I overridden
protected $signature = 'make:api-controller';
and after run
php artisan make:api-controller MyNewController
I got error
No arguments expected for "make:api-controller" command, got "MyNewController".
What is the problem?
Take a look at the ControllerMakeCommand, they use
protected $name = 'make:controller';
You probably have this:
protected $signature = 'make:api-controller';
So in your new class, replace $signature with $name.

Why does php artisan migrate fresh --seed is not working properly

Hi developers i have question regarding on php artisan migrate:refresh --seed VS php artisan db:seed I just wanted to ask because I have problem on php artisan migrate:refresh --seed, however when I use the php artisan db::seed it works properly
Now the data that I Created on my seeder is not seeding to the tables. I don't know why where the problem came from
Seeding: VehicleModelsSeeder
Seeded: VehicleModelsSeeder (0 seconds)
Here is my vehicle model seeder class
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use App\Vehicle;
use App\VehicleModel;
class VehicleModelsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
//
$vehicles = Vehicle::all();
foreach($vehicles as $vehicles_data) {
VehicleModel::forceCreate([
'name' => Str::random(5),
'vehicle_id' => $vehicles_data->id
]);
}
}
}
By default, the db:seed command runs the Database\Seeders\DatabaseSeeder class.
There is two solutions:
1. You need to call your additional seeder(s) in the default seeder's run method.
database/seeders/DatabaseSeeder.php
public function run()
{
$this->call([
VehicleModelsSeeder::class
]);
}
Then:
php artisan migrate:refresh --seed
2. You can specify which seeder you want to run with the --class flag, but in this case you need to run the refresh and the migrate commands separately:
php artisan migrate:refresh
php artisan db:seed --class:VehicleModelsSeeder
More info: Laravel database seeding documentation

ErrorException running php artisan vendor:publish with Elqouent-Sluggable

I'm using Laravel 5 and attempting to install Eloquent-Sluggable. I have followed all the steps in the installation instructions:
composer require cviebrock/eloquent-sluggable
composer update
Add Cviebrock\EloquentSluggable\SluggableServiceProvider::class to the Providers in config/app.php
php artisan vendor:publish
The last step resulted in this error message:
[ErrorException] Argument 2 passed to
Cviebrock\EloquentSluggable\SluggableTableCommand::__construct() must be an instance of Illuminate\Foundation\Composer, instance of
Illuminate\Support\Composer given, called in
C:\wamp\www\blog\vendor\cvi
ebrock\eloquent-sluggable\src\SluggableServiceProvider.php on line 92 and defined
What caused the error to occur?
It happened to me too. This is because the SluggableTableCommand requires Composer instance in its constructor but accidentally different type of instance is passed.
This might not be the smartest way to fix this but if you are in rush you can just remove the Composer declaration before $composer in the SluggableTableCommand constructor. The file that you need to edit is vendor/cviebrock/eloquent-sluggable/src/SluggableTableCommand.php on line 44
This should work:
/**
* Create a new migration sluggable instance.
*
* #param SluggableMigrationCreator $creator
* #param Composer $composer
*/
public function __construct(
SluggableMigrationCreator $creator,
$composer
) {
parent::__construct();
$this->creator = $creator;
$this->composer = $composer;
}
I had the same error using Laravel 5.2.29 when I tried to create migration via console command
php artisan sluggable:table posts slug
What solved the issue was creation usual Laravel migration, but I had commented 'Cviebrock\EloquentSluggable\SluggableServiceProvider::class' row in config/app.php file before.
php artisan make:migration add_slug_to_posts_table
with the next code in migration file
public function up(){
Schema::table('posts', function(Blueprint $table){
$table->string('slug')->nullable();
});
}
public function down(){
Schema::table('posts', function(Blueprint $table){
$table->dropColumn('slug');
});
}
and ran the migration
php artisan migrate

Laravel 5 getstream.io getUserFeed()

I'm trying to use getstream.io in my Laravel 5 application. I'm following the tutorial here, but got stuck on this one:
$feed = FeedManager::getUserFeed($user->id);
When I go to the FeedManager class, I couldn't find the getUserFeed() method. Here's how my FeedManager class look like:
<?php namespace GetStream\StreamLaravel\Facades;
use Illuminate\Support\Facades\Facade;
class FeedManager extends Facade {
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor() { return 'feed_manager'; }
}
I wonder if I did something wrong during installation. The tutorial said to run php artisan config:publish get-stream/stream-laravel, but I did php artisan vendor:publish get-stream/stream-laravel. The reason is because I got an error when running config:publish, so I used vendor:publish instead
The Stream-PHP-Example is now working in Laravel 5, take a look: https://github.com/GetStream/Stream-Example-PHP

Resources