Home > Database > Mysql Tutorial > Laravel Eloquent ORM in Bangla Part-enerating Model Classes)

Laravel Eloquent ORM in Bangla Part-enerating Model Classes)

Mary-Kate Olsen
Release: 2025-01-05 17:32:45
Original
684 people have browsed it

Laravel Eloquent ORM in Bangla Part-enerating Model Classes)

Eloquent: Generating Model Classes is a process for generating model classes for interacting with database tables in Laravel. Using Eloquent ORM (Object-Relational Mapper) you can easily read, create, update and delete data from database tables.


How to create Model Class

1. Creating Model using Artisan command

The model class is created using Laravel's artisan CLI. Run the following command:

php artisan make:model ModelName

Copy after login
Copy after login
Copy after login

For example, to create a model named Post:

php artisan make:model Post

Copy after login
Copy after login
Copy after login

This will create a Post.php file in the app/Models directory.


The basic structure of the model

The

model should generally look like this:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;
}

Copy after login
Copy after login
Copy after login

2. Creating model with migration file

Use the following command to create the model and database migration together:

php artisan make:model Post -m

Copy after login
Copy after login

This will create two things:

  1. Post model.
  2. A migration file named database/migrations/2025_01_05_000000_create_posts_table.php.

3. Creating Factory and Seeder with Model

To build models with Factory and Seeder files:

php artisan make:model ModelName

Copy after login
Copy after login
Copy after login
  • m: will generate migration.
  • f: will create the factory.
  • s: will generate cedar.
  • c: will create the controller.

relation to database table in model

1. Default table name

Laravel assumes the database table name according to the model class name.
For example:

  • The default table for the Post model will be posts.
  • The default table for the User model will be users.

If you want to use custom tables, set the $table property on the model:

php artisan make:model Post

Copy after login
Copy after login
Copy after login

Important properties and methods of model

1. $fillable and $guarded

$fillable or $guarded is used to determine which fields in the database can insert data.

  • $fillable: Data can be inserted into specified fields.
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;
}

Copy after login
Copy after login
Copy after login
  • $guarded: Data cannot be inserted into specified fields.
php artisan make:model Post -m

Copy after login
Copy after login

2. $primaryKey

If the primary key of your table is something other than id:

php artisan make:model Post -mfsc

Copy after login

3. $timestamps

Laravel uses table created_at and updated_at columns by default. If you don't want to use them:

class Post extends Model
{
    protected $table = 'blog_posts';
}

Copy after login

4. Relationships (Relationships)

Models can be linked to each other using Eloquent relationships.

  • One-to-One Relationship:
php artisan make:model ModelName

Copy after login
Copy after login
Copy after login
  • One-to-Many relationship:
php artisan make:model Post

Copy after login
Copy after login
Copy after login
  • Many-to-Many relationship:
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;
}

Copy after login
Copy after login
Copy after login

The above is the detailed content of Laravel Eloquent ORM in Bangla Part-enerating Model Classes). For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template