Home > Backend Development > PHP Tutorial > Laravel Eloquent ORM in Bangla Part-Deleting Models)

Laravel Eloquent ORM in Bangla Part-Deleting Models)

DDD
Release: 2025-01-16 20:05:09
Original
245 people have browsed it

Laravel Eloquent ORM in Bangla Part-Deleting Models)

Detailed below are different methods to delete records from database using Laravel Eloquent ORM. It provides easy and efficient way to delete data.

1. Deleting a single record:

delete() method using:

A particular record can be deleted from the database using

delete() method.

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

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

// রেকর্ড মুছে ফেলুন
$post->delete();</code>
Copy after login
Copy after login

Remember: After deletion, the record will no longer be in the database.

2. Deleting multiple records:

Using the

method with the wheredelete() clause:

To delete multiple records, use the where method with the delete() clause.

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

// 'draft' স্ট্যাটাসের সকল রেকর্ড মুছে ফেলুন
Post::where('status', 'draft')->delete();</code>
Copy after login
Copy after login

3. Using the destroy() method:

One or more records can be deleted using the

destroy() method.

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

// একক রেকর্ড মুছে ফেলুন
Post::destroy(1);

// একাধিক রেকর্ড মুছে ফেলুন
Post::destroy([2, 3, 4]);</code>
Copy after login
Copy after login

Remember: The destroy() method directly deletes records using the primary key.

4. Using model event:

Model Events are triggered when Eloquent models are deleted.

  • event: Triggered before the record is deleted.deleting
  • Event: Triggered after record is deleted.deleted
<code class="language-php">class Post extends Model
{
    protected static function booted()
    {
        static::deleting(function ($post) {
            // মুছে ফেলার আগে করণীয় কাজ
            Log::info('Post is being deleted: ' . $post->id);
        });

        static::deleted(function ($post) {
            // মুছে ফেলার পর করণীয় কাজ
            Log::info('Post deleted: ' . $post->id);
        });
    }
}</code>
Copy after login
Copy after login

5. Soft Deletes:

What is soft delete?

Soft delete records are not physically deleted by appending a timestamp to the

column.deleted_at

To use soft delete:

The

trait must be added to the SoftDeletes model.

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

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

// রেকর্ড মুছে ফেলুন
$post->delete();</code>
Copy after login
Copy after login

Soft delete:

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

// 'draft' স্ট্যাটাসের সকল রেকর্ড মুছে ফেলুন
Post::where('status', 'draft')->delete();</code>
Copy after login
Copy after login

Recovering soft deleted records:

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

// একক রেকর্ড মুছে ফেলুন
Post::destroy(1);

// একাধিক রেকর্ড মুছে ফেলুন
Post::destroy([2, 3, 4]);</code>
Copy after login
Copy after login

Force Delete:

<code class="language-php">class Post extends Model
{
    protected static function booted()
    {
        static::deleting(function ($post) {
            // মুছে ফেলার আগে করণীয় কাজ
            Log::info('Post is being deleted: ' . $post->id);
        });

        static::deleted(function ($post) {
            // মুছে ফেলার পর করণীয় কাজ
            Log::info('Post deleted: ' . $post->id);
        });
    }
}</code>
Copy after login
Copy after login

Querying soft deleted records:

  • withTrashed(): Returns both soft deleted and non-deleted records.
  • onlyTrashed(): will restore only soft deleted records.

6. Direct deletion from database:

Records can be deleted directly with SQL queries using Laravel's DB facade.

<code class="language-php">use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];
}</code>
Copy after login

The above is the detailed content of Laravel Eloquent ORM in Bangla Part-Deleting 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template