diarsa/laravel-where-like

A Laravel package that simplifies searching across multiple fields, including related models, using Laravel's query builder in an efficient way.

Fund package maintenance!
diarsa

Installs: 21

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/diarsa/laravel-where-like

v1.1.3 2025-10-16 02:02 UTC

This package is auto-updated.

Last update: 2025-10-16 02:09:06 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Search using Laravel's query builder across multiple fields, including related models, in a simple and efficient way.

Installation

You can install the package via composer:

composer require diarsa/laravel-where-like

Usage

Without whereLike (Traditional Way)

Without this package, you would need to write complex queries with multiple orWhere and orWhereHas clauses:

$search = $request->search;
return User::where('status', 1)
    ->where(function ($query) use ($search) {
        $query->where('name', 'LIKE', "%{$search}%")
              ->orWhere('address', 'LIKE', "%{$search}%")
              ->orWhereHas('categoryDetail', function ($query) use ($search) {
                  $query->where('category_code', 'LIKE', "%{$search}%");
              })
              ->orWhereHas('histories', function ($query) use ($search) {
                  $query->where('device', 'LIKE', "%{$search}%");
              });
    })
    ->get();

With whereLike (Simplified Way)

With this package, you can perform searches across multiple fields, including related models, using this simple method:

$search = $request->search;
return User::where('status', 1)
    ->whereLike(
        ['name', 'address', 'categoryDetail.category_code', 'histories.device'], 
        $search
    )
    ->get();

More Examples

Searching in Multiple Fields

// Search in user's basic fields
User::whereLike(['name', 'email', 'phone'], $searchTerm)->get();

Searching in Related Models

// Search in user and their profile information
User::whereLike([
    'name', 
    'email', 
    'profile.bio', 
    'profile.location',
    'posts.title',
    'posts.content'
], $searchTerm)->get();

Searching with Additional Conditions

// Combine with other query conditions
User::where('status', 'active')
    ->where('created_at', '>=', now()->subMonth())
    ->whereLike(['name', 'email', 'profile.bio'], $searchTerm)
    ->orderBy('created_at', 'desc')
    ->paginate(10);

Additional Features

Mask Sensitive Data

The package also provides a maskSensitiveData macro for Laravel Collections to help you mask sensitive information:

use Illuminate\Support\Collection;

$sensitiveData = collect(['john.doe@email.com', '081234567890', 'SecretPassword123']);

// Default masking (shows first 2 and last 2 characters)
$masked = $sensitiveData->map(function ($item) {
    return collect()->maskSensitiveData($item);
});
// Result: ['jo***@email.com', '08******7890', 'Se**********123']

// Custom masking (first 3 and last 1 characters)
$customMasked = $sensitiveData->map(function ($item) {
    return collect()->maskSensitiveData($item, 3, 1);
});
// Result: ['joh******.com', '083*******0', 'Sec**********3']

Requirements

  • PHP 8.1, 8.2, 8.3, or 8.4
  • Laravel 9.x, 10.x, 11.x, or 12.x

Another methods.

$laravelWhereLike = new Diarsa\LaravelWhereLike\LaravelWhereLike();
echo $laravelWhereLike->tambah(1, 4);

use Diarsa\LaravelWhereLike\LaravelWhereLike;
Route::get('calculate', function() {
    return LaravelWhereLike::tambah(1, 4);
});

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Support us

You can support us by Buy me a coffee.

Credits

License

The MIT License (MIT). Please see License File for more information.