In Laravel, what's the difference between a regular controller and resource controller? Please provide examples to illustrate the differences.
There is mainly no difference..It a special type of controller.
When you create a controller like this
php artisan make:controller YourNameController --resource
it auto create some function like index, create, store, show, edit, update, destroy. basically for crud.
For details go to documentation https://laravel.com/docs/7.x/controllers#resource-controllers
Well, the only difference is this.
The resource controller creates a PHP file with already defined CRUD operations while a regular controller creates an empty file.
To create a regular controller with the name UserController
php artisan make:controller UserController
To create a resource controller
php artisan make:controller UserController --resource
Hope it was helpful
Use this for more resources https://laravel.com/docs/9.x/controllers
Related
I am new in Laravel and I want to create a model with all it's basic properties like fillable, table etc. using artisan.
I have explored some blogs but I have not found any way to create a model with basic fields. So, Is there any command to do it like we can do it in CodeIgniter 4?
There is no automatic way to create table fields using artisan, what you can do instead is create the following and modify it:
php artisan make:model Blog -mcr
Read more on model options: https://laravel.com/docs/9.x/eloquent#generating-model-classes
The above will create:
A model named "blog"
A migration file - this is where you can modify/add fields to the database
A resource controller named "BlogController"
For migrations, you need to edit your migration file that it generated, see the documentation on how to do this: https://laravel.com/docs/9.x/migrations#migration-structure
Once you finish you can run the command:
php artisan migrate
to push the changes of the migration file to the database.
Alternatively, you can always see what kind of options you have in artisan by doing:
php artisan make:model -help
And if you want to create everything related to the model, do the command:
php artisan make:model Blog -a
Generate a migration, seeder, factory, policy, resource controller,
and form request classes for the model
i am using this
php artisan make:model Category -mcr`
Its fine but i want this resource controller
with model parameter in different folder.
Thank you for help/suggestions.
I am on Laravel 5.8. Need to make model to custom folder. So I tried some modification of make:model command but without success.
make:model Books --path=app/Model throws --path option does not exists
make:model Books app/Model throws too many arguments
So what is the right way to do it please? Thank you.
If you want to create a model in a different path you can for example use:
php artisan make:model Model/Book
This will create your model Book in a folder /app/Model/. If the folder does not exist, it will be created.
Note: I'd suggest using the singular Book instead of Books as model name
I have this great doubt, if I copy and modify an existing controller in my laravel project vs create a controller with php artisan make:controller MyController, Is there any affected configuration files? or is it normal to copy and edit my other controllers?
It's just a lot easier to do the official command
php artisan make:controller myController
I mean, it's what they are there for. I don't believe that hand-making a controller will have a negative effect but it's so much easier and safe to use the function that's already defined.
many times in a controller you have a lot of logic that you want to reuse, IMHO sometimes it's worth manually copying the controller.
I am new to Laravel 5 and gone through https://laravel.com/docs/5.8/controllers but could not find any way in which i could create controller using php artisan with a specific method name. I tried below options but none of them work.
php artisan make:controller HomeControler#index logout
and
php artisan make:controller HomeControler/index logout
and
php artisan make:controller HomeController index logout
Is it possible in Laravel 5 to achieve this? If yes, is it also possible to declare more than one function while creating controller using artisan make command?
php artisan make:controller HomeControler --resource
The controller will contain a method for each of the available resource operations:
index
create
store
show
edit
update
destroy
You can't generate Controller and specified the name of method which will be generate. The only way to generated controller with all CRUD methods It is when you generate a resource based controller
php artisan make:controller PersonController --resource
The other way is to generate Controller which is empty