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

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

Related

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

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

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