How to automatically run the migrations in a Laravel package? - laravel

I just installed Cartalyst's Sentry 2 in a Laravel 4 application but I found out that I have to run that package's migrations separately by specifying --package=cartalyst/sentry, which makes automatic deployment impossible.
Is there a way to run php artisan migrate and have it run Sentry's migrations as well?

After Laravel 5 there is a better way to solve this:
Create your migrations in package's
/database/migrations folder
.
After that, in package's service provider create a boot method, referencing the migrations folder
public function boot()
{
$this->loadMigrationsFrom(__DIR__ .
'/database/migrations');
}
Now, on the top level folder (the main app, which required this package), you can run the default migrate command ( php artisan migrate ) and it will automatically find the packages migrations througth the loadMigrationsFrom method
ANSWER COPIED FROM:
http://voerro.com/en/tutorials/r/developing-and-distributing-laravel-5-packages/3

What I usually do in a scenario like this is publish the package migrations though the command:
php artisan migrate:publish vendor/package
This copies the migration files from any given package to your migrations folder.

I created a composer script to replace php artisan migrate ...... The script runs my migrations and the vendor's migrations everything at once.
My composer scripts is
"scripts": {
"migrate": [
"php artisan migrate --env=$LARAVEL_ENV",
"php artisan migrate --package=\"cartalyst/sentry\" --env=$LARAVEL_ENV",
"php artisan migrate --package=\"mrjuliuss/syntara\" --env=$LARAVEL_ENV",
"php artisan migrate --package=\"filmoteca/static-pages\" --env=$LARAVEL_ENV"
]
}
Then you can run the migration with LARAVEL_ENV=prod composer run-script migrate
To pass parameter to the script I use environment variables. In the previous example I set the environment variable LARAVEL_ENV to prod so the migration use the production database connection.
You can always create a alias in your local machine to short the command. For example alias migrate="LARAVEL_ENV=local composer run-script migrate"
I think this approach is good because when you are going to add a new package to your composer.json and this package has a migration you add the package and the package's migration in the same file. So, you do not forget add/remove the migration of a package.
This is a complete composer.json with the script

The 3rd party package must declare a Service Provider, that declare migrations.
As you can see at https://github.com/laravel/passport/blob/9.x/src/PassportServiceProvider.php#L80:
protected function registerMigrations()
{
if (Passport::$runsMigrations && ! config('passport.client_uuids')) {
return $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}

Related

Creating Schema automatically using migration in Laravel

Is it possible to create schemas automatically in the database when xampp is turning on? There is no information about it in the Laravel documentation, documentation says only how to do it manually by writing the command php artisan migrate.
Xampp includes a script called apache_startup.bat in the root directory, you can add the following line to it
c:\xampp\php\php.exe -f /path/to/laravel/app/artisan migrate
Then when the apache server starts, it should run that the equivalent of php artisan migrate to setup the database.
You can do php artisan make:migration [migration-name] - other than that, you have to fill the rest yourself.

Laravel create migration file in specific folder

I've created Payment package for my laravel project
I want to make migration files inside migrations folder of my package. How can create it using artisan command?
I want something like
php artisan make:migration packages/Payment/src/Database/add_orderId_to_cart_payment_table
Use this command on the root folder:
create the only Migration file:
php artisan make:migration create_products_table --create=products
Create Migration, Model file:
php artisan make:model Product -m
For Create Migration,Model,Controller file:
php artisan make:model Product -mcr
If you want to do it manually then you may set --path as per your folder requirement.
php artisan make:migration filename --path=/app/database/migrations/relations
php artisan make:migration filename --path=/app/database/migrations/translations
If you want to migrate then:
php artisan migrate --path="/app/database/migrations/relations"
For specific directories:
php artisan make:migration create_users_table --path=/packages/Payment/src/Database
The new migration will be placed in your packages/Payment/src/Database directory.
For Running Migrations: php artisan migrate --path=/packages/Payment/src/Database
Generating a migration using the inbuilt framework feature:
php artisan make:migration create_users_table
The new migration will be placed in your database/migrations directory.
For Running Migrations: php artisan migrate
you need to publish them from your package to your migrations folder like this in your package service provider boot method:
$this->publishes([
__DIR__.'/Database/migrations/' => database_path('migrations'),
], 'migrations');
run this command php artisan vendor:publish --tag=migrations
and after that you can run php artisan migrate
You can run:
php artisan make:migration --help and see all the options available for you to use.
It seems you are making a package. It has nothing to do with where to create migrations, just create migrations in regular way, run your migrations and simply put them in your package/src/Database/migrations/yourMigrations_xxxxxx.php and in your package serivce provider in boot() method write this line
$this->loadMigrationsFrom(__DIR__ . '/Database/migrations');
laravel has a built in method loadMigrationsFrom('path/to/migrations') to pick up migrations, when someone installs this package and when he'll run php artisan migrate it'll run all the migrations whether they are in the user's Database/migrations or in your package's package/src/Database/migrations/2022_03_29_23424_create_countries_table_.php, all the migrations will run

Laravel passport migration not found

I installed laravel passport using command compser require laravel/passsport,
but after this, no new migration was made in database/migrations directory.
Why is that?
The migration files exist in the Passport package in your vendor directory.
The migrations won't be copied over to your database/migrations folder as they don't need to be since they're registered by the PassportServiceProvider.
You should just be able to run php artisan migrate and they will be included.
If you're using Laravel 5.3 or 5.4 then you will need to register the service provide in your app/config.php file.
If you want to modify passport migration or just want it inside your migrations folder you can publish passport migrations files to your migrations folder using publish command:
php artisan vendor:publish --tag=passport-migrations
Make sure you have PassportServiceProvider registered in config/app.php
// config/app.php
Laravel\Passport\PassportServiceProvider::class,
You can also delete from migrations table specific migration(s). Running php artisan migrate again should recreate specific migrations only.

How can I migrate my new migration file in Laravel?

sorry for silly question but I'm really facing problem. After migrating all the migration files. When I need another features for my project I have to make another table but When I'm going to migrate the new table using "php artisan migrate:rollback" or "php artisan migrate:rollback step=17" .... all the migration files are migrating this time too and I am losing all previous data. Then How can I migrate only the new one?
To migrate only new migrations is simple: php artisan migrate. The way the process works is that Laravel creates a table in your database called migrations. In that table are the names of the migrations that have been run already. If there are new migrations the above command will work.
You can read more about running migrations in the documentation.
To migrate any new migration created php artisan migrate is the artisan command.
To get the details of all commands php artisan is the artisan command.
To get the details of any specific command php artisan help command-name-here.
In the case of creating a new file, you only need to run php artisan migrate again.
If you want modify original migration file and migrate it again, you can use this package to migrate.
First, install package in your Laravel project:
composer require caloskao/migrate-specific
Register command at app/Console/Kernel.php :
protected $commands = [
\CalosKao\MigrateSpecific::class
];
Now, run this command to migrate your file
php artisan migrate:specific database/migrations/table.php

db seeds from a package Laravel 4

I am developing a package for an application with Laravel 4, I added some seeders into the folder seeds of folder database of my application, what is the right command line to run the seeder ?
I tried some similar commands to db:migrate to run these seeders like:
php artisan db:seed --package=Packagename/PackageDirectory
but it seems they are not working here, what is the right command line to run these seeders insdie the package from root of application?
According to the documentation, the only available option for this function is --class.
What you can do is create a class that gets data from your package.

Resources