Create Controller, Model, and Migration Laravel in One Command

Create Controller, Model, and Migration Laravel in One Command

To create model, migration and controller in one command in laravel, simply open your cmd or terminal and navigate to your laravel project directory using cd laravel-app command and then run php artisan make:model YourFileName -mcr in it. Or to create model and migration file only, you should use php artisan make:model fileName -mr the command.

How to make Model, Migration, and Controller in One Command in Laravel

Here are some commands and code examples to create controller, model and migration laravel in one command in laravel 11, 10, 9:

1. Create model command

To create model, run php artisan make:model ModelName cmd or terminal and press enter:

php artisan make:model Photo

This command is to create the photo model. After the created the model, it looks like this:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
    //
}

2. Create Controller command

To create a controller, run php artisan make:controller along with controller name on cmd or terminal:

 php artisan make:controller PhotoController

This command will create controller named photoController. It looks like as follow:

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class PhotoController extends Controller
{
}

3. Create a Resource Controller Command

To create the resource controller in laravel, use the following command:

php artisan make:controller PhotoController --resource

PHP artisan make controller resource command creates a resource controller. It has already created some methods like index, update, edit, destroy, etc. It looks like this:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PhotoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

4. Make Model and Controller in Laravel

Use the php artisan make:model -mc for creating a controller and model, you can use this command as follow:

 php artisan make:model -mc Photo

This single command has been created as a photo controller and model.

5. Create model, migration and Controller in laravel in one command

To create a model, controller, and migration in Laravel in one command, simply run php artisan make:model YourFileName with -mcr command to make it:

php artisan make:model YourFileName -mcr

Please replace YourFileName with your file name.

6. Create model and migration in laravel in one command

To create a model and migration in Laravel in one command, simply run php artisan make:model YourFileName with -mr command to make it:

php artisan make:model YourFileName -mr

Please replace YourFileName with your file name.

Conclusion

In this tutorial, you have successfully learned how to create a controller and model. Also, learn how to create a model and resource controller using one command.

Recommended Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *