Laravel make model with migration - laravel

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

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

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

How to create a controller in a sub-folder when defining as a parameter?

I'm creating a project with Laravel and I'm aware that there is a way to create the migration and controller (with or without resources) along with the model with php artisan make:model ExampleModel -m -c -r.
In my project I have models and controllers in sub-folders. I'm able to add the model and controllers to sub-folders when creating them separately (php artisan make:model Models/example_model and same in controllers). But I don't know how to add the controllers to a sub-folder when creating them with a parameter on the make:model command.
I really appreciate any help you can provide.
I've tried the below commands already and they don't appear to be working
php artisan make:model Models/example -m -MyControllers/c -r
php artisan make:model Models/example -m MyControllers/-c -r
This will create sub-folder for both models and controller and files into it
php artisan make:controller TestFolder\TestController --model=Models\TestModel
I hope this helps.
Happy Coding :)
I'm not sure it's possible with the make:model command. My best guess is to use 2 commands:
php artisan make:model Models/NewModel -m
php artisan make:controller MyControllers/NewModelController -r

Refresh laravel migration for specific table

Can I run php artisan migrate:refresh for specific table?
Or can I refresh specific table migration in general?
I tried this:
php artisan migrate --path=/database/migrations/selected/
But it's not working!
For Specific File run this command:
php artisan migrate:refresh --path="database\migrations\Your_Migration_File_Name_table.php"
Here --file= is for location of your file and migrate:refresh will empty your table data
If you want to empty all table's data from database then run
php artisan migrate:refresh command.
This works for me:
The --table and --create options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options pre-fill the generated migration stub file with the specified table
php artisan make:migration create_users_table --create=users
php artisan make:migration add_votes_to_users_table --table=user
Source: https://laravel.com/docs/5.6/migrations

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

Resources