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. 


 


Laravel: Adding new column/columns to the existing table in a migration

Adding New Column to an existing table

In this article, you will learn the step-by-step procedure to add single or multiple columns in our existing database tables using Laravel migration.


Step 1: To get started, open the command prompt or terminal and navigate to the root directory of our Laravel project.



Step 2: Type the following command to create a new migration file:


php artisan make:migration add_phone_number_to_schools


This code will create an add_type_to_notes file in the migration folder. it will look like this-



Now, replace the column_name with the name of the column you want to add and the table_name with the name of the table you want to modify.


Step 3: Open the newly created migration file in the database/migrations folder. Inside the up function, add the code to create the new column or columns:


<?php


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class AddPhoneNumberToSchools extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

         Schema::table('schools', function (Blueprint $table) {

            $table->string('phone_number', 10)->nullable(false)->default(null);

        });

    }


    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

        Schema::table('schools', function (Blueprint $table) {

            $table->dropColumn('phone_number');

        });

    }

}


Step 4: Run the migration command.


Initially, our screen will look like the below image. 


As soon as we run the migration command a new column will be added and our database will look like the below image.



To make our database look like the above image and add a new column, we have to add the below code to our database.


php artisan migrate

C:\xampp\htdocs\laraveltest>php artisan migrate

Migrating: 2023_05_01_055549_add_phone_number_to_schools

Migrated: 2023_05_01_055549_add_phone_number_to_schools (21.14ms)

C:\xampp\htdocs\laraveltest>


Adding Multiple Columns to an Existing Table using Laravel Migration

To add multiple columns to the existing table 


Step 1: Navigate to the root directory and enter the following command

php artisan make:migration add_multiple_column_to_schools


Step 2: As we want to add multiple columns to the existing table, we have to introduce the column name in our code. 


Use the below command to introduce new column names. 


<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;


class School extends Model

{

    protected $fillable = [

        'school_name', 'email', 'country', 'phone_number', 'Afiliated_by', 'School_location', 'Medium'

    ];

}



Step 3: Adding code to the newly added migration file.



<?php


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class AddMultipleColumnToSchools extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        Schema::table('schools', function (Blueprint $table) {

            $table->string('afiliated_by')->nullable();

            $table->string('short_description')->nullable();

           $table->string('medium')->nullable()

        });

    }


    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

        Schema::table('schools', function (Blueprint $table) {

            $table->dropColumn(['afiliated_by''short_description', 'medium']);

        });

    }

}



Step 4: Run the below command and check the database:


php artisan migrate



The final image shows that we have successfully added multiple columns in the existing table using Laravel migration.


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...