Laravel create migration file in specific folder - laravel

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

Related

Laravel: Create an API Controller, model and migration in one line

This is what I use at the moment to create Controller and Model
php artisan make:controller API/name_of_controller --api --model=name_of_model
then create a migration
php artisan make:migration create_users_table
In the past before I started using API, I used to do this to create model, migration and controller in one single line
php artisan make:model Banana -mcr
Is there a way to do this with API controller?
I think there is no existing command to do that. How you create them currently is the best solution
php artisan make:controller API/TestController --api --model=Test
# then
php artisan make:migration create_tests_table
Why?
php artisan help make:controller
php artisan help make:model
Currently there is no option to include a migration file when
creating a controller first
And there is no option to specify a
controllers name (e.g. --controller=API/TestController when using
make:model command
Run this command
php artisan make:model Banana -mcr
-m, --migration Create a new migration file for the model.
-c, --controller Create a new controller for the model.
-r, --resource Indicates if the generated controller should be a resource controller
php artisan make:model Banana -mcr
Banana created successfully.
Created Migration: 2017_06_03_150652_create_bananas_table
BananaController created successfully.
OR
php artisan make:model Banana -a
where -a = all
Run this command
php artisan make:model Banana -a --api

How do I migrate:refresh a specific table in my database?

I want to migrate:refresh a specific table called scores. I tried this:
php artisan migrate:refresh --path=/database/migrations/scores
but it says nothing to migrate.
You cannot refresh a single migration if its not the last one, but you can step back in the migrations chain.
Assuming that you have five migrations and your table is at fourth place you can do like this:
php artisan migrate:refresh --step=2
With this command you refresh the last two migrations.
Note that each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
You can use the --path argument but it should be a directory in wich the command searches for migrations not a single migration file.
If you run:
php artisan help migrate:refresh
You can see all the parameters accepted.
yes, you can do this by using Specify the path to the migrations.
Follow these steps:
first of all to create a migration, use the make:migration command:
php artisan make:migration create_scores1_table
php artisan make:migration create_scores2_table
php artisan make:migration create_scores3_table
The new migration will be placed in your database/migrations directory. Each migration file name contains a timestamp like this:
2019_09_04_045427_create_scores1_table.php
2019_09_04_045500_create_scores2_table.php
2019_09_04_045518_create_scores3_table.php
Now, you want to refresh a specific table(for example: scores2 table), so you can be used like this:
php artisan migrate:refresh --path=database/migrations/2019_09_04_045500_create_scores2_table.php
It only refresh the scores2 table and not all the rest of the table
Yes you can migrate:refresh selected migrations. You left off the .php extension from your line of code.
It should look like this example
php artisan migrate:refresh --path=/database/migrations/2020_09_28_224702_create_addresses_table.php
So, if scores is your migration file name, add .php
php artisan migrate:refresh --path=/database/migrations/scores

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

Zizaco entrust does not create the entrust.php

Why the file entrust.php is not created when I run this:
php artisan vendor:publish
I'm following this config and this is my composer.json
"zizaco/entrust": "5.2.x-dev"
"You can also publish the configuration for this package to further customize table names and model namespaces.
Just use php artisan vendor:publish and a entrust.php file will be created in your app/config directory."
But the file entrust.php is not created.
What can I do? This is odd.
In the link you provided they say and, if it does not appear, manually copy the /vendor/zizaco/entrust/src/config/config.php file in your config directory and rename it entrust.php.
just try this
add
"zizaco/entrust": "5.2.x-dev" in composer.json
then composer update.
Then in your config/app.php add
Zizaco\Entrust\EntrustServiceProvider::class,
in the providers array and
'Entrust' => Zizaco\Entrust\EntrustFacade::class,
Next try in your terminal to publish and see in your config
php artisan vendor:publish
--provider="Zizaco\Entrust\EntrustServiceProvider"
check link for how to use zizaco
I solved it by running
php artisan config:cache
Before running
php artisan vendor:publish
run this command
php artisan config:cache
then run
php artisan vendor:publish

Laravel make model with migration

I'm creating a model on the Laravel 5 with this command:
php artisan make:model Settings
As it shows on the video lessons that as soon as model is created, new migration file also must be created. But, in my situtation, migration is not being created.
How can I get the migration generated when model is created?
composer.json:
...
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*"
},
...
I'm guessing your build of L5 is fairly old, as they disabled creating migrations alongside the model creation.
Try running the ff:
php artisan make:model Settings --migration
Try using this Command
php artisan make:model ModelName -m
OR
php artisan make:model ModelName --migration
It will create model with migration class
Also you can use this command:
php artisan make:model ModelName -m
and if you wanna make controller for your model, you should write this command :
php artisan make:model ModelName -mc
// or
php artisan make:model ModelName -mcr //(r: for resource methods)
2020 update:
You can now do:
php artisan make:model ModelName -a
To create a:
model
controller
seeder
migration
factory
All using one command.
You can use the make:model flags like;
php artisan make:model Setting -m
Help: php artisan make:model --help
Create Model and Migration :
php artisan make:model Image -m
Create Model, Migration and Controller with resource :
php artisan make:model Image -mcr
Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests :
php artisan make:model Image --all
It's weird because to skip migration you could use the flag --no-migration. That means that calling php artisan make:model Foo should automatically create everything. Does artisan show any errors? Did you check logs? What Laravel version are you using? 5? 5.1?
If you would like to generate a database migration when you generate the model, you may use the --migration or -m option
php artisan make:model Settings --migration
php artisan make:model Settings -m

Resources