Sunday, September 22, 2024
Google search engine
HomeLanguagesCreate Model Controller and Migration Laravel in One Command

Create Model Controller and Migration Laravel in One Command

In this laravel create controller and model using php artisan make:model and php artisan make:controller commands on command line. Here, you will learn how to create a controller Or resource controller and model using the command line (CLI). You will also learn how to create a controller and model using one command only. This example tutorial also work with laravel version.

Laravel 10, 9, 8 Create Model, Migration, and Controller in One Command

Let’s use the following ways to Create controller, model and migration laravel in one command in laravel 10, 9, 8:

1. Create model command

Use the php artisan make model for creating a model using the command line (CLI) :

 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

Use the php artisan make:controller command for creating a controller using this command line:

 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. Command For Create Model and Controller

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. Laravel make:model with migration and controller

If you want to create controller and model, so you can execute php artisan make:model -mc for creating a controller and model in command prompt:

 php artisan make:model Product -mcr

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

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

RELATED ARTICLES

Most Popular

Recent Comments