Pages

Make new Model/Controller/Migration in Laravel

 In this article, we have included steps to create a model and controller or resource controller with the help of command line(CLI). Here we will tell you the process to create a model and controller using the command only. Use this tutorial for the Laravel version. 


The below ways will help you create controller, migration, and model Laravel using just one command line in Laravel. 


  1. Create model command

You can use php artisan make model to create a model with the help of the command line (CLI):


php artisan make:model Photo


Now, the above command will help you create the photo model. After creating the model, the code will look like the following:


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model

{

    //

}


  1. Create controller command

You can use the php artisan make controller command to create a controller with the help of command line:

php artisan make:controller PhotoController


Here, you will create a new controller named photoController. After creating controller it will look like the following:

<?php

  

namespace App\Http\Controllers;

use Illuminate\Http\Request;

  

class PhotoController extends Controller

{

}


  1. Create a Resource Controller Command

You can use the below command to create the resource controller in Laravel. 


php artisan make:controller PhotoController --resource


A resource controller is created using the PHP artisan make controller resource command for Laravel. It has already developed a number of methods, including index, update, edit, and delete. It appears as follows:


<?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)

    {

        //

    }

}


  1. Command to create Controller and Model

You can use the php artisan makde:model -mc to create controller and model, the following command will help you:


php artisan make:model -mc Photo


The above single command will create a photo model and controller.


  1. Laravel makde:model with controller and migration

You may use the command prompt to construct a controller and model by running php artisan make:model -mc if you want to:


php artisan make:model Product -mcr


This single command has been developed as a model and product controller.


You have successfully completed this tutorial and learnt how to build a controller and model. A model and resource controller can be created using just one command, as well. With the help of this example you can create migration with model in Laravel. 


 


No comments:

Post a Comment

Make new Model/Controller/Migration in Laravel

  In this article, we have included steps to create a model and controller or resource controller with the help of command line(CLI). Here w...