PHP code example of anthonyedmonds / laravel-find

1. Go to this page and download the library: Download anthonyedmonds/laravel-find library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anthonyedmonds / laravel-find example snippets


return [
    /* The key and label for searching all models; set as false to disable */
    'anything-key' => 'any',
    'anything-label' => 'Anything',
    
    /* A table that is guaranteed to exist */
    'base-table' => 'users',
    
    /* Which models can be searched for, and its display label */
    'models' => [
        'users' => \App\Models\User::class,
    ],
];

class Author extends Model
{
    use Findable;

    public static function findDisplayLabel(): string
    {
        return 'Authors by name and book title';
    }

    public static function canBeFoundBy(?Model $user): bool
    {
        return true;
    }
    
    protected static function findLabel(): string
    {
        return 'name';
    }

    protected static function findDescription(): string
    {
        return '"An author"';
    }

    protected static function findLink(): string
    {
        return route('authors.show', '~id');
    }

    protected static function findFilters(Builder $query, string $term): Builder
    {
        return $query->where('name', 'LIKE', "%$term%");
    }
}

protected static function findFilters(Builder $query, string $term, ?Model $user = null): Builder
{
    return $query
        ->leftJoin('books', 'books.id', '=', 'chapters.book_id')
        ->where('chapters.title', 'LIKE', "%$term%")
        ->orWhere('books.title', 'LIKE', "%$term%");
}

$query = Find::findBy('my-term', 'users');
$query->orderBy('users.name');
$query->paginate(10);

public function results(FindRequest $request): View
{
    // Show results...
}

FindDate::term('1/3/2024'); // 2024-03-01%
FindDate::term('01/03'); // %03-01%
FindDate::term('2024'); // 2024-%
FindDate::term('03'); // false
FindDate::term('potato'); // false

FindDate::term('8/12'); // %12-08%, works for both year-month and month-day