Laravel How to create a model

Laravel Model Creation: Learn how to create your own custom model with an example.

Creating a Model in Laravel

A model is a PHP class that represents the data in a database table. It allows you to access and manipulate the data in an object-oriented way. Laravel provides an easy way to create models and use them in your application. In this tutorial, we will show you how to create a model in Laravel.

Step 1: Create a Model File

First, we need to create a model file. Models are usually stored in the app directory. To create a model, we need to use the make:model command:

php artisan make:model ModelName

This command will create a model file in the app directory with the given name. The model class will be generated with some basic code:

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class ModelName extends Model
{
    //
}

We can add our own methods and properties to this class. For example, if we want to access the data from a database table called users, we can add the following code to the model class:

protected $table = 'users';

Step 2: Create a Migration File

Before we can use the model, we need to create a database table for it. To do this, we need to create a migration file. A migration file is a PHP script that contains the SQL commands to create, alter, and delete a database table. To create a migration file, we need to use the make:migration command:

php artisan make:migration create_users_table

This command will create a migration file in the database/migrations directory with the given name. The migration file will contain the following code:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

We can customize this file by adding the columns that we need. For example, if we want to add a name and email column, we can add the following code to the up() method:

$table->string('name');
$table->string('email')->unique();

Step 3: Run the Migration

Now that we have created the migration file, we need to run it to create the database table. To do this, we need to use the migrate command:

php artisan migrate

This command will create the database table based on the migration file. After running the command, we should see the new database table in our database.

Step 4: Use the Model

Now that we have created the database table and the model, we can start using it in our application. To do this, we need to use the model class. For example, if we want to get all the users from the database, we can use the all() method:

$users = AppModelName::all();

This will return all the users from the database. We can also use the model to create, update, and delete records from the database. To learn more about models, please refer to the Laravel documentation.

In this tutorial, we have shown you how to create a model in Laravel. We have also shown you how to create a migration file and run it to create the database table. We have also shown you how to use the model to access the data in the database.

Answers (0)