Laravel Eloquent Relationship withTrashed() Method | websolutioncode.com
Laravel Eloquent Relationship withTrashed() Method | websolutioncode.com

Laravel Eloquent Relationship withTrashed() Method

In Laravel’s Eloquent ORM (Object-Relational Mapping), the withTrashed() method is used to retrieve all records from a table, including the soft-deleted ones. Soft deleting is a feature that allows you to “delete” records while keeping them in the database, marking them as “deleted” rather than physically removing them.

This is particularly useful when you want to retain the information for historical or auditing purposes, but still, want to exclude these soft-deleted records from most queries by default.

Implementation in Eloquent Relationships

Let’s explore how withTrashed() works within Eloquent relationships.

Consider you have two models, for instance, Post and Comment, where Comment belongs to a Post.

Here are the model definitions:

// Post.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;

    protected $fillable = ['title', 'content'];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}
// Comment.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Comment extends Model
{
    use SoftDeletes;

    protected $fillable = ['post_id', 'content'];

    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

Using withTrashed() in Relationships

Suppose you want to retrieve a post along with its associated comments, including the soft-deleted comments. Here’s how you can achieve this:

$post = Post::withTrashed()->find($postId);

// Retrieving comments including soft-deleted ones
$comments = $post->comments()->withTrashed()->get();

In the above code snippet, $postId is the identifier of the post you want to retrieve. withTrashed() is used both on the Post and Comment models to fetch the post along with its comments, including the soft-deleted ones.

Example Scenario

Let’s create a scenario to illustrate this concept:

  1. Create a post:
$post = Post::create([
    'title' => 'Sample Post Title',
    'content' => 'Lorem ipsum dolor sit amet.'
]);

Add a comment to the post:

$comment = $post->comments()->create([
    'content' => 'This is a comment on the post.'
]);

Soft delete the comment:

$comment->delete();

Retrieve the post with comments including soft-deleted ones:

$post = Post::withTrashed()->find($postId);
$comments = $post->comments()->withTrashed()->get();

In this scenario, the $comments collection will contain the soft-deleted comment along with any active comments associated with the post.

Conclusion

The withTrashed() method in Laravel’s Eloquent relationships is a powerful tool when dealing with soft-deleted records. It allows you to include soft-deleted records in your query results, providing more flexibility in managing and displaying data while respecting the soft-delete functionality.

Remember, soft deletes are often used to maintain data integrity and history within an application, ensuring that deleted records can be recovered or referenced if needed.

Check our tools website Word count
Check our tools website check More tutorial

Leave a Reply