How to Create a Controller And Model Laravel 6 Using cmd

Overview

In this laravel 6 tutorial – you will learn how to create a controller and how to create a model using the command line. You will also learn how to create a controller and model using one command only.

Create

Lets’s start

Create model command

Use the below command for creating model using the command line

 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;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
    //
}

Create Controller command

Use the below command for creating a controller using the command line

 php artisan make:controller PhotoController

This command created a photoController. It looks like this

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

}

Create a Resource Controller

php artisan make:controller PhotoController --resource

This command create 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)
    {
        //
    }
}

One Command For Create Model and Controller

Use the below command for creating a controller and model using one single command:

 php artisan make:model -mc Photo

This single command has been created as a photo 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.



Leave a Reply

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