Home > Backend Development > PHP Tutorial > Laravel Eloquent ORM in Bangla Part-Inserting and Updating Models)

Laravel Eloquent ORM in Bangla Part-Inserting and Updating Models)

Barbara Streisand
Release: 2025-01-16 20:09:11
Original
503 people have browsed it

Laravel Eloquent ORM in Bangla Part-Inserting and Updating Models)

Laravel Eloquent ORM can be used to add new data and update existing data in the database. It works in a simple and natural manner. Let's discuss in detail.

Adding Data (Inserting Models)

1. Create new record using save() method

The save() method is used to create and save Eloquent models.

<code class="language-php">use App\Models\Post;

// নতুন পোস্ট তৈরি
$post = new Post();
$post->title = 'নতুন ব্লগ পোস্ট';
$post->content = 'এটি পোস্টের বিষয়বস্তু।';
$post->status = 'draft';

// ডেটা সংরক্ষণ
$post->save();</code>
Copy after login
Copy after login
The

save() method creates a new record in the database.

2. create() method using the

shorthand method The

create() method inserts data into one line.

<code class="language-php">use App\Models\Post;

Post::create([
    'title' => 'দ্রুত ব্লগ পোস্ট',
    'content' => 'এটি পোস্টের বিষয়বস্তু।',
    'status' => 'published',
]);</code>
Copy after login
Copy after login

Remember: To use create() you must define the fillable or guarded property in your model.

<code class="language-php">class Post extends Model
{
    protected $fillable = ['title', 'content', 'status'];
}</code>
Copy after login

3. Adding multiple records (Mass Insert)

Multiple records can be inserted at once using the

insert() method.

<code class="language-php">use App\Models\Post;

Post::insert([
    ['title' => 'পোস্ট ১', 'content' => 'বিষয়বস্তু ১', 'status' => 'published'],
    ['title' => 'পোস্ট ২', 'content' => 'বিষয়বস্তু ২', 'status' => 'draft'],
]);</code>
Copy after login

Updating Data (Updating Models)

1. Updating specific records using the save() method

A model's data can be updated by fetching it from the database.

<code class="language-php">use App\Models\Post;

// রেকর্ড খুঁজে বের করা
$post = Post::find(1);

// ডেটা আপডেট করা
$post->title = 'আপডেট করা ব্লগ পোস্ট';
$post->status = 'published';

// সংরক্ষণ
$post->save();</code>
Copy after login

2. update() is updated using the

method The

update() method is used to update multiple columns simultaneously.

<code class="language-php">use App\Models\Post;

Post::where('id', 1)->update([
    'title' => 'আপডেট করা শিরোনাম',
    'status' => 'published',
]);</code>
Copy after login

3. Updating multiple records at once

Multiple records can be updated using

update().

<code class="language-php">use App\Models\Post;

// নতুন পোস্ট তৈরি
$post = new Post();
$post->title = 'নতুন ব্লগ পোস্ট';
$post->content = 'এটি পোস্টের বিষয়বস্তু।';
$post->status = 'draft';

// ডেটা সংরক্ষণ
$post->save();</code>
Copy after login
Copy after login
Adding or updating data using the

upsert() method

upsert() method is used to add new data or update existing data.

<code class="language-php">use App\Models\Post;

Post::create([
    'title' => 'দ্রুত ব্লগ পোস্ট',
    'content' => 'এটি পোস্টের বিষয়বস্তু।',
    'status' => 'published',
]);</code>
Copy after login
Copy after login

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

source:php.cn
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