Laravel - create model, controller and migration in single artisan command - laravel

I can create a model and resource controller (binded to model) with the following command
php artisan make:controller TodoController --resource --model=Todo
I want to also create a migration with the above command, is it possible?

You can do it if you start from the model
php artisan make:model Todo -mcr
if you run php artisan make:model --help you can see all the available options
-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
Update
As mentioned in the comments by #arun in newer versions of laravel > 5.6 it is possible to run following command:
php artisan make:model Todo -a
-a, --all Generate a migration, factory, and resource
controller for the model

Updated
Laravel 6 or Later
Through the model
To Generate a migration, seeder, factory and resource controller for the model
php artisan make:model Todo -a
Or
php artisan make:model Todo -all
Other Options
-c, --controller Create a new controller for the model
-f, --factory Create a new factory for the model
--force Create the class even if the model already exists
-m, --migration Create a new migration file for the model
-s, --seed Create a new seeder file for the model
-p, --pivot Indicates if the generated model should be a custom intermediate table model
-r, --resource Indicates if the generated controller should be a resource controller
For More Help
php artisan make:model Todo -help
Hope Newbies will get help.

You can do it with the following command:
php artisan make:model post -mcr
Brief :
-m, to create migration
-c to create controller
-r to specify the controller has resource

You can make model + migration + controller, all in one line, using this command:
php artisan make:model --migration --controller test
Short version: php artisan make:model -mc test
Output :-
Model created successfully.
Created Migration:2018_03_10_002331_create_tests_table
Controller created successfully.
If you need to perform all CRUD operations in the controller then use this command:
php artisan make:model --migration --controller test --resource
Short version: php artisan make:model -mc test --resource

php artisan make:model PurchaseRequest -crm
The Result is
Model created successfully.
Created Migration: 2018_11_11_011541_create_purchase_requests_table
Controller created successfully.
Just use -crm instead of -mcr

php artisan make:model Author -cfmsr
-c, --controller Create a new controller for the model
-f, --factory Create a new factory for the model
-m, --migration Create a new migration file for the model
-s, --seed Create a new seeder file for the model
-r, --resource Indicates if the generated controller should be a resource controller

Laravel 5.4 You can use
php artisan make:model --migration --controller --resource Test
This will create
1) Model
2) controller with default resource function
3) Migration file
And Got Answer
Model created successfully.
Created Migration: 2018_04_30_055346_create_tests_table
Controller created successfully.

We can use php artisan make:model Todo -a to create model, migration, resource controller and factory

You don't need to add --resource flag just type the following and laravel will create the whole desired resources
php artisan make:controller TodoController --model=todo

Instead of using long command like
php artisan make:model <Model Name> --migration --controller --resource
for make migration, model and controller, you may use even shorter as -mcr.
php artisan make:model <Model Name> -mcr
For more MOST USEFUL LARAVEL ARTISAN MAKE COMMANDS LISTS

If you are using Laravel as an only API add --api option:
php artisan make:model Post -a --api

To make mode, controllers with resources, You can type CMD as follows :
php artisan make:model Todo -mcr
or you can check by typing
php artisan help make:model
where you can get all the ideas

You can use -m -c -r to make migration, model and controller.
php artisan make:model Post -m -c -r

To make all 3: Model, Controller & Migration Schema of table
write in your console: php artisan make:model NameOfYourModel -mcr

How I was doing it until now:
php artisan make:model Customer
php artisan make:controller CustomersController --resource
Apparently, there’s a quicker way:
php artisan make:controller CustomersController --model=Customer

php artisan make:model modelname -mcr
to create model. Here -mcr stands for migrations components and resourses

Related

How to generate "make:model -a" with directory for seeders and controller and migrate name?

How to generate "make:model -a" with directory for seeders and controller and migrate name?
Laravel Framework 8.44.0
I am generating a model php artisan make:model Blog/MyCategory -a and expect to see the following structure:
Controllers/Blog/MyCategoryController.php
Models/Blog/MyCategory.php
factories/Blog/MyCategory.php
mifrations/2021_06_01_042639_create_blog_my_categories_table.php
seeders/Blog/MyCategorySeeder.php
Execute the command php artisan make:model Blog/Category -a
Model created successfully.
Factory created successfully.
Created Migration: 2021_06_01_044253_create_my_categories_table
Seeder created successfully.
Controller created successfully.
but it creates
Controllers/MyCategoryController.php (NO)
Models/Blog/MyCategory.php (YES)
factories/Blog/MyCategory.php (YES)
mifrations/2021_06_01_042639_create_my_categories_table.php (NO)
seeders/MyCategorySeeder.php (NO)
This way I cannot generate two MyCategory.
Execute the command php artisan make:model Shop/MyCategory -a
Model created successfully.
Factory created successfully.
InvalidArgumentException
A CreateMyCategoriesTable class already exists.
at vendor/laravel/framework/src/Illuminate/Database/Migrations/MigrationCreator.php:102
Remove the
model Shop/MyCategory.php,
factory Shop/MyCategoryFactory.php
migration file 2021_06_01_044253_create_my_categories_table.php
Now let's create the correct migration file
php artisan make:migration CreateBlogMyCategoryTable
Again execute the command php artisan make:model Shop/MyCategory -a
Model created successfully.
Factory created successfully.
Created Migration: 2021_06_01_050039_create_my_categories_table
Seeder already exists!
Controller already exists!
It again creates a 2021_06_01_050039_create_my_categories_table file and does not take into account the model in the Shop directory
Remove generated files again:
model Shop/MyCategory.php
factory Shop/MyCategoryFactory.php
migration file 2021_06_01_050039_create_my_categories_table.php
controller MyCategoryController.php
Now let's create the correct migration and controllers:
php artisan make:migration CreateShopMyCategoryTable
php artisan make:controller Blog/MyCategoryController
php artisan make:controller Shop/MyCategoryController
Total
Thus, we see that the "-а" option is not suitable in this case. You need to create models, controllers and migrations separately.
php artisan make:controller Blog/MyCategoryController -r
php artisan make:controller Shop/MyCategoryController -r
php artisan make:migration CreateBlogMyCategoryTable
php artisan make:migration CreateShopMyCategoryTable
Model with factory
php artisan make:model Blog/MyCategory -f
php artisan make:model Shop/MyCategory -f
This command also makes the correct Factory
php artisan make:factory Blog\\MyCategoryFactory --model=Blog\\MyCategory
<?php
namespace Database\Factories\Blog;
use App\Models\Blog\MyCategory;
use Illuminate\Database\Eloquent\Factories\Factory;
class MyCategoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = MyCategory::class;
// ....
}
The migration files, models, and controllers as we need in the appropriate directories.
php artisan migrate
Migration table created successfully.
...
Migrating: 2021_06_01_055036_create_blog_my_category_table
Migrated: 2021_06_01_055036_create_blog_my_category_table (36.26ms)
Migrating: 2021_06_01_055541_create_shop_my_category_table
Migrated: 2021_06_01_055541_create_shop_my_category_table (39.16ms)
As I understand it, the required structure will still have to be done by separate commands.
But then another problem appeared: I do not understand how to use now factory()->create()
Route::get('/', function () {
\App\Models\Blog\MyCategory::factory(1)->create();
return view('welcome');
});
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'larablog.my_categories' doesn't exist (SQL: insert into `my_categories` (`updated_at`, `created_at`) values (2021-06-01 06:59:52, 2021-06-01 06:59:52))
or tinker
php artisan tinker
Psy Shell v0.10.8 (PHP 7.4.18 — cli) by Justin Hileman
>>> MyCategory::factory()->create()
PHP Error: Class 'MyCategory' not found in Psy Shell code on line 1
>>> Blog\MyCategory::factory()->create()
PHP Error: Class 'Blog\MyCategory' not found in Psy Shell code on line 1
>>> \Blog\MyCategory::factory()->create()
PHP Error: Class 'Blog\MyCategory' not found in Psy Shell code on line 1
>>> App\Models\Blog\MyCategory::factory()->create()
Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'larablog.my_categories' doesn't exist (SQL: insert into `my_categories` (`updated_at`, `created_at`) values (2021-06-01 06:48:26, 2021-06-01 06:48:26))'
>>> \App\Models\Blog\MyCategory::factory()->create()
Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'larablog.my_categories' doesn't exist (SQL: insert into `my_categories` (`updated_at`, `created_at`) values (2021-06-01 06:48:29, 2021-06-01 06:48:29))'
How to make such a structure work?
Sometimes it is necessary to clear the cache, this command worked in my project:
composer dump-autoload
Good luck!
use php artisan make:model ModelName -mcr -a
it will create all that you need. migration, seeder, factory, controller and model

Why is artisan make:controller not making controllers?

$ artisan make:controller fooController
Controller created successfully.
A controller is not created under app\Http\Controllers and git status does not show any changes. Running the command a second time returns "Controller already exists!" artisan make:model Foo works fine as does creating a controller by hand, but that's not really fun. What am I missing?
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
The workstation is Windows and there do not seem to be any permission problems.
Edit: Tried running composer update, same result.
Rename your controller name using capital on the first letter:
artisan make:controller FooController
Also, according to Laravel naming convention, you don't have to add Controller at the end of the controller name. Use this instead:
artisan make:controller Foo
make sure your controller name is in singular form
For Example:
php artisan make:model Supplier --migration --controller
will produce:
-Controller named: SupplierController.php
-Model named: Supplier.php
-Migrations named: 2017_06_17_161642_create_suppliers_table.php (laravel automatically change it into plural form)
Also, try checking your laravel version using: php artisan --version
and make sure its 5.4.xx
if not, update your laravel using composer update

Laravel 5.4 - Artisan make:controller XxxController --resource --model=Xxx not identifying Model driectory genereted by reliese/laravel

I have a database that already is in use when I started a new API in Laravel 5.4.
For this reason, instead of use Migrations, I have used reliese/laravel to generate the Models from my database.
The point is that reliese have created models inside app/Models/. So I have a table that was converted into app/Models/City.php for example.
So when I try to create a Controller using Artisan like this:
php artisan make:controller CityController --resource --model=City
I get this error:
A App\City model does not exist. Do you want to generate it? (yes/no) [yes]:
Because Artisan is searching the Model City.php inside app/ folder.
Is there a way to make Artisan to point to app/Models instead?
This is more elegant
php artisan make:controller CityController --resource --model=Models/City
I will share here what I did:
php artisan make:controller CityController --resource --model=Models\\City
Controller created successfully.
I had to use double backslash "\" without the app\ folder.

Create model with resourceful controller

I know I can create a model with controller by using the command php artisan make:model Task -cand I also can create a resourceful controller with php artisan make:controller TasksController -r. Is there a way to create both a model with a resourceful controller?
Yes, you can do this without using packages. If you run php artisan make:model --help you will find the options that you can add to the command.
php artisan make:model --help
Options:
-c, --controller Create a new controller for the model.
-r, --resource Indicated if the generated controller should be a resource controller
So if you run it with both the c and the r flag, it will generate the model, along with a resource controller:
php artisan make:model Task -c -r
Note: this works for versions >=5.3!
You may want to look at a generator package.
https://github.com/amranidev/scaffold-interface
https://github.com/InfyOmLabs/laravel-generator
I suggest a simple method which 100% works for me in laravel 7
php artisan make:model ModelName -mr
This command will create a new model with resourceful controller as well as with migration
-m denotes for migrations
-r creates resourceful controller and associate it with model
hope this is usefull for you
example
php artisan make:model Product -c
-a, --all Generate a migration, seeder, factory, and resource controller for the model
-c, --controller Create a new controller for the model
-f, --factory Create a new factory for the model
--force Create the class even if the model already exists
-m, --migration Create a new migration file for the model
-s, --seed Create a new seeder file for the model
-p, --pivot Indicates if the generated model should be a custom intermediate table model
-r, --resource Indicates if the generated controller should be a resource controller
--api Indicates if the generated controller should be an API controller
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

How to use artisan to make views in laravel 5.1

I have been looking in the docs for a solution to make views with basic CURD operations but without much success.
I guess this might be pretty simple, but am missing something or not looking hard enough.
i can make models and controllers using the below artisan commands
php artisan make:model modelName
php artisan make:controller controllerName
But how do we make the basic CURD views. something like
php artisan make:views
cant find any doc for this. Please help
At the time of writing, there isn't a way to create views via artisan without writing your own command or using a third party package. You could write your own as already suggested or use sven/artisan-view.
if you are using laravel 5.1 or 5.2 this gist can help you make:view command just create command copy and paste the code from gist.
Step 1:
php artisan make:command MakeViewCommand
Step 2:
copy class from this gist
https://gist.github.com/umefarooq/ebc617dbf88260db1448
Laravel officially doesn't have any Artisan cammands for views.
But you could add third party plugins like Artisan View
Here's the link Artisan View
After adding this plugin to your project by the guide provided here you should be able to perform following cammands :
Create a view 'index.blade.php' in the default directory
$ php artisan make:view index
Create a view 'index.blade.php' in a subdirectory ('pages')
$ php artisan make:view pages.index
Create a view with a different file extension ('index.html')
$ php artisan make:view index --extension=html
There is very easy way to create a view(blade) file with php artisan make:view {view-name} command using Laravel More Command Package.
First Install Laravel More Command
composer require theanik/laravel-more-command --dev
Then Run
php artisan make:view {view-name}
For example
It create index.blade.php in resource/views directory
php artisan make:view index
It create index.blade.php in resource/views/user directory
php artisan make:view user/index
Thank you.
In v5.4 you need to create the command with:
php artisan make:command MakeView
and before you can use it, it must be registered in App/Console/Kernel like
protected $commands = [
Commands\MakeView::class
];
then you make a view like: php artisan make:view posts/create
To create a view (blade) file through command in laravel 8:
composer require theanik/laravel-more-command --dev
php artisan make:view abc.blade.php
You can install sven/artisan-view package to make view from CMD, to install package write this command:
composer require sven/artisan-view --dev
After installing it, you can make a single view or folder with all views that contain {index-create-update-show}
To make a single file we using this command:
php artisan make:view Name_of_view
For example
php artisan make:view index
To make a folder that contain all resources index - create - update - show write name of folder that contain all this files for example:
php artisan make:view Name_of_Folder -r
For example:
php artisan make:view blog -r
-r is a shorthand for --resource you can write full name or shorthand to make resource.
you can extend yields from master page if master page inside in directory layouts we write command sith this format
php artisan make:view index --extends=layouts.master --with-yields
layouts is a directory this directory may be with a different name in your project the idea is name_of_folder/master_page that you want to extend yields from it.
For more view docs

Resources