Current Location: Home> Latest Articles> Laravel Model Soft Deleting, Left Join and Table Alias Usage Example

Laravel Model Soft Deleting, Left Join and Table Alias Usage Example

gitbox 2025-07-29

Laravel Model Soft Deleting, Left Join and Table Alias Usage Example

Laravel is a popular PHP framework that comes with a powerful Eloquent ORM tool, allowing you to represent database tables through classes and simplify data operations. During development, you may often need to perform more complex queries such as soft deletes and left joins. In this article, we will explore how to implement these features in Laravel models.

Soft Deleting

Soft deleting is a logical deletion technique where a specific field (like `deleted_at`) is set to indicate that the data has been logically deleted, rather than physically removed. This approach allows data to be safely restored when needed. Laravel provides an easy-to-use mechanism for soft deleting, as shown in the following code:

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

class Post extends Model
{
    use SoftDeletes;
}

Once `SoftDeletes` is added, you can use soft delete features. For example, you can use the `withTrashed` method to query data that has been soft-deleted:

<span class="fun">$posts = Post::withTrashed()->get();</span>

You can also restore soft-deleted data using the `restore` method:

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

Left Join

A left join allows you to fetch data from both the left and right tables in a query. Even if there is no match between the data from the right table and the left table, the data from the left table will still be returned, and the corresponding fields from the right table will be set to NULL. In Laravel, you can easily use left joins like this:

$users = DB::table('users')
             ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
             ->select('users.*', 'posts.title')
             ->get();

This query returns an array of all users along with their corresponding post titles.

Table Alias

When performing complex queries across multiple tables, using table aliases is very helpful. It reduces the complexity of the code and enhances its readability. In Laravel, table aliases are used in a similar way to SQL. For example:

$results = DB::table('users')
            ->join('contacts', function ($join) {
                $join->on('users.id', '=', 'contacts.user_id')
                     ->where('contacts.user_id', '>', 5);
            })
            ->select('users.*', 'contacts.phone')
            ->get();

This query demonstrates how to use a `join` to connect the `users` and `contacts` tables and how to apply table aliases to simplify the query.

Conclusion

This article has covered how to use soft deleting, left joins, and table aliases in Laravel models. Soft deleting protects data from accidental deletion, left joins allow for more complex queries across multiple tables, and table aliases help simplify the code. These techniques not only improve code quality but also increase development efficiency.