Laravel Cashier - publish migration results in "cannot declare class CreateCustomersColumns" - laravel

I have a fresh Laravel installation and I've added Cashier to my project.
Since the Users model on my app won't have a stripe connection, but rather an Accounts model, I need to alter their migration to add columns to Accounts instead of Users
The documentation says to run:
php artisan vendor:publish --tag="cashier-migrations"
which adds the two migration files to database/migrations
From there I can change users to accounts in the migration file.
When I try to run php artisan migrate, I get:
Whoops\Exception\ErrorException : Cannot declare class CreateCustomerColumns, because the name is already in use
This problem only goes away when I delete the migration files, but then the new columns are added to users.

The documentation states that you can disable their migration files by putting Cashier::ignoreMigrations(); in AppServiceProvider
I didn't realize that's what I wanted to do. I thought the publish command only published the two files I needed to edit, however, those are the only migration files that come with Cashier.
Be sure to add Cashier::ignoreMigrations(); in the register method.
And add use Laravel\Cashier\Cashier;

The file (and thus the class) must exist twice, probably in your database/migrations directory, most likely with 2 different datetime prefixes on the filenames.

Related

Change default directory for creating migration in Laravel

I want to change the default directory for creating migration i.e. If I do php artisan make:migration create_users_table, it should create users table in /database/migrations/child_database. I do not want to use --path every time I create a migration for child_database. I want to change the default directory for php artisan make:migration.
you can create your own artisan command to achieve this:
create your a new artisan command and use it to make migration in your custom directory using this article. hope help you ^_^
try this package github repo or use laracast link
You can use loadMigrationsFrom() in your AppServiceProvider.
Inside the boot() function, run:
/**
* Register Custom Migration Paths
*/
$this->loadMigrationsFrom([
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder1',
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder2',
]);/
This question has been answered before, even though it's a little bit old it still has it's value.
Have a look at: https://stackoverflow.com/a/35895106
If you use this answer, the default directory is changed and you will have the solution you want.

How to create modular in laravel?

I want to create a modular to create a controller and a model.
Of course, the model is pre-created.
php artisan module:make-controller Admin\ReportController Report --model=Report
I see this message
The "--model" option does not exist.
I assume you used nWidart Modules, so the answer is you can not use --model option because nWidart Modules doesn't support that option/parameter. You need run another command to create a model, an example:
php artisan module:make-model Report Admin
Report is model name, and Admin is module name
check it out for more module commands

How does Laravel keeping track of batch value?

I created some migration files which created the table "users" and "user_info".
Running php artisan migrate created the tables and added them to the migrations table in the database.
Then I needed to create a new column and add some new rows to the user_info table. So I created a new migration file with the same name as the first migration file which was used to create the "user_info" table.
Since Laravel adds a timestamp to the file I didn't think that would create any problems. However, in the the new migration file I used the same class name as for the the first migration file, did the changes I wanted and finally ran php artisan migrate again.
That seemed to create problems since I got an error saying that the class name was already in use.
So I changed the migration filename to be user_info2 and used the class name UserInfo2 and tried again.
This time it worked and the batch has been bumped up to 2 in the migration table in the database.
But how does Laravel know that the migration file actually is related to the first user_info migration file since I changed both the filename and class name? I thought they had to have the same name in order to make Laravel have control of the "batch-value".
Thanks for any help!
The batch number of the migrations is used to specify multiple migrations that were created while running php artisan migrate.
So for example you create two migrations: users and user_info you run php artisan migrate this two migrations will be created and will be referenced by the same batch number: 1.
Then you create another migration, when you migrate this migration will have the number 2 as batch number.
When you do php artisan migrate:rollback based on the batch number Laravel knows which migrations to rollback, in this case only one migration with the batch number of 2.
So batch in this terms means an array of migrations that has not been run. Those will be referenced by the same ID, in order to make the rollback possible.
Hope this explains a bit more.

How to override one function in multiple commands?

We are in the process of migrating an old application to Laravel. The old application manages different projects and has an "admin" database for the main application and creates a new database for each project. We would prefer to keep this structure but it creates some problems for things such as migrations.
Initially we created our own base command that implements the handle() function so that any command that extends this command will loop through all the project databases. This means we have changed the structure so that migrations are separated into two directories since we have "admin" and "project" migrations for the different types of databases. We also duplicate each Laravel command that is already there if it doesn't work with our implementation out of the box.
However this creates a lot of duplicate code as we made our own implementation of the migrate command that simply passes calls to Laravels own migrate command. I now find myself having to implement the --force option into our new command for example.
So what I would like to do is override the handle() function in all the Laravel console commands in some way. Is there a better way of implementing this?
You can create your own base class for your artisan classes and extends your commands from it.

how to create migration from existing database in laravel

I am new to laravel. i know command to create database from laravel migration.
I have already created database in mysql. so how can i convert migration from that database.
You could use Jeffrey Way's generator tool for Laravel 4, and do them by hand. It is a very useful tool. https://github.com/JeffreyWay/Laravel-4-Generators (if you are using Laravel 5, use this package instead: https://github.com/laracasts/Laravel-5-Generators-Extended)
Or you could try out one of the following packages, they should convert your existing schema to Laravel migrations. I have not tried them, but have a look:
https://github.com/adamkearsley/laravel-convert-migrations
https://github.com/barryvdh/laravel-migration-generator
Also, read the following answer in another question, might be useful to you or others: Reverse engineering or Auto-Generations of Entities in Laravel?
For modern versions of Laravel you should probably use a package like https://github.com/kitloong/laravel-migrations-generator
I quick wrote this python script to make migration files out of an existing database.
https://github.com/aljorhythm/sql-to-laravel-migrations
After set up, just do
python retrieve-and-convert.py
If the accepted answer (at the time of editing this post) does not work for you, use this simple approach.
Check out this package by Kitloong is really easy to use.
https://github.com/kitloong/laravel-migrations-generator
After installation, just run something like this to export the migration to a location of your choice:
php artisan migrate:generate --path="path\to\existing\folder"
Example:
php artisan migrate:generate --path="C:\xampp\htdocs\laravel_bs4\database\migrations\my_new_migrations"
You should end up with migrations created from the database and saved in the location you have specified.
You can omit the --path option and simply run
php artisan migrate:generate
This would export it to the default Laravel's migration folder (which is something I usually do not want).
Be sure to follow the prompts. Sticking to the default is usually okay.
How about this?
Migrations Generator.It's for Laravel, 4, 5, 6, 7, but I don't know if it is for Laravel 8
Simple solution, to use online Laravel migration generator for your existing SQL table schema without installing any package. Just enter your SQL schema and get Laravel migration file.
Try: https://laravelarticle.com/laravel-migration-generator-online
If you don't mind using Doctrine 2 in your project instead of Eloquent that has reverse engineering built in.
For laravel 9 follow below link
https://laravel.com/docs/9.x/migrations#generating-migrations

Resources