PHP code example of bayareawebpro / searchable-resource

1. Go to this page and download the library: Download bayareawebpro/searchable-resource 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/ */

    

bayareawebpro / searchable-resource example snippets


SearchableResource::make(User::query());

SearchableResource::make(User::query())
	->orderable(['name', 'email'])
	->orderBy('name')
	->sort('desc')
	->paginate(16);

use App\User;
use App\Queries\UserSearch;
use App\Queries\RoleFilter;
use App\Http\Resources\UserResource;
use BayAreaWebPro\SearchableResource\SearchableResource;
use BayAreaWebPro\SearchableResource\SearchableBuilder;

SearchableResource::make(User::query())
    ->resource(UserResource::class)
    ->queries([
        UserSearch::class,
        RoleFilter::class
    ])
    ->orderable([
        'id', 'name', 'email', 'role',
        'created_at', 'updated_at',
    ])
    ->appendable([
        'created_for_humans',
        'updated_for_humans',
    ])
    ->select([
        'id',
        'name', 'email', 'role',
        'created_at', 'updated_at',
    ])
    ->rules([
       'my_filter_key' => '

public function index()
{
    $resource = SearchableResource::make(User::query())
        ->query(Users::make())
        ->orderable(['name', 'email'])
        ->orderBy('name')
        ->sort('desc')
        ->paginate(5)
        ->execute()
    ;
    return view('users.index', [
        'items' =>$resource->getItems(),
        'search' =>$resource->getSearch(),
        'order_by' =>$resource->getOrderBy(),
        'per_page' =>$resource->getPerPage(),
        'options' =>$resource->getOptions(),
        'sort' =>$resource->getSort(),
    ]);
}

SearchableResource::make(User::query())->resource(UserResource::class);

 declare(strict_types=1);
 
namespace App\Queries;
 
use Illuminate\Database\Eloquent\Builder;
use BayAreaWebPro\SearchableResource\AbstractQuery;
 
class LikeQuery extends AbstractQuery
{
    public string $field = 'search';
    protected string $attribute = 'name';
    public function __invoke(Builder $builder): void
    {
        $builder->where($this->attribute, "like", "%{$this->getValue($this->field)}%");
    }
}

SearchableResource::make(User::query())
    ->query(
        LikeQuery::make()
            ->field('search')
            ->attribute('last_name')
    )
    ->query(
        SelectQuery::make()
            ->field('role')
            ->attribute('role')
            ->options(['admin', 'customer'])
     )
;

 declare(strict_types=1);

namespace App\Queries;
 
use BayAreaWebPro\SearchableResource\AbstractQuery;
use BayAreaWebPro\SearchableResource\Contracts\ConditionalQuery;
 
class ConditionalRoleQuery extends AbstractQuery implements ConditionalQuery
{
    public string $field = 'role';
    protected string $attribute = 'role';
 
    public function __invoke(Builder $builder): void
    {
        $builder->where($this->attribute, $this->getValue($this->field));
    }

    public function getApplies(): bool
    {
    	return parent::getApplies(); // Customize with $this->request
    }
}

 declare(strict_types=1);

namespace App\Queries;
 
use BayAreaWebPro\SearchableResource\AbstractQuery;
use BayAreaWebPro\SearchableResource\Contracts\ValidatableQuery;
 
class ConditionalRoleQuery extends AbstractQuery implements ValidatableQuery
{

    public string $role = 'role';
    public string $admins = 'only_admins';
    
    protected string $attribute = 'role';
 
    public function __invoke(Builder $builder): void
    {
        $builder->where($this->attribute, $this->getValue($this->admins) ?: $this->getValue($this->role));
    }

    public function getRules(): array
    {
        return [
           $this->role => [
               '

 declare(strict_types=1);

namespace App\Queries;
 
use BayAreaWebPro\SearchableResource\AbstractQuery;
use BayAreaWebPro\SearchableResource\Contracts\ProvidesOptions;
 
class ProvidesOptionsQuery extends AbstractQuery implements ProvidesOptions
{

    public string $field = 'role';
    protected string $attribute = 'role';
 
    public function __invoke(Builder $builder): void
    {
        $builder->where($this->attribute, $this->getValue($this->field));
    }

    public function getOptions(): array
    {
        return [
            $this->field => [
                'admin', 'editor'
            ],
        ];
    }
}

SearchableResource::make(User::query())->useFormatter(new OptionsFormatter);

 declare(strict_types=1);

namespace App\Http\Resources\Formatters;

use Illuminate\Support\Collection;
use BayAreaWebPro\SearchableResource\OptionsFormatter as Formatter;

class OptionsFormatter extends Formatter {

    /**
     * @param string $key
     * @param Collection $options
     * @return Collection
     */
    public function __invoke(string $key, Collection $options): Collection
    {
        if($key === 'abilities'){
            return $this->nullable($this->literal($options));
        }
        if($key === 'role'){
            return $this->nullable($this->titleCase($options));
        }
        return $this->baseOptions($key, $options);
    }
}


use BayAreaWebPro\SearchableResource\OptionsFormatter;
use BayAreaWebPro\SearchableResource\SearchableBuilder;

$this->app->resolving(
    SearchableBuilder::class,
    function (SearchableBuilder $builder){
    return $builder
        ->useFormatter(new OptionsFormatter)
        ->labeled(request()->hasSession())
        ->orderBy('created_at')
        ->paginate(8)
        ->sort('desc')
    ;
});

use App\Queries\RoleQuery;

SearchableResource::make(User::query())
	->queries([
		RoleQuery::class
	]);


use App\Queries\RoleQuery;

SearchableResource::make(User::query())
	->query(RoleQuery::make());

SearchableResource::make(User::query())
    ->appendable([
        'created_for_humans',
        'updated_for_humans',
        'bytes_for_humans',
    ]);

SearchableResource::make(User::query())
    ->with([
        'my_key' => []
    ]);

SearchableResource::make(User::query())
    ->fields([
        'my_filter_state'
    ]);


class SessionEnabledQuery{
    public function __invoke(SearchableBuilder $builder): void 
    {
        $builder->labeled();
    }
}

SearchableResource::make(User::query())
    ->when(request()->hasSession(), new SessionEnabledQuery)
    ->when(request()->hasSession(), function(SearchableBuilder $builder){
        $builder->labeled();
    })
;


use BayAreaWebPro\SearchableResource\SearchableBuilder;
use BayAreaWebPro\SearchableResource\Contracts\InvokableBuilder;
class UserSearchable implements InvokableBuilder{
    public function __invoke(SearchableBuilder $builder): void 
    {
        $builder->queries([
            RoleQuery::class
        ]);
    }
}

SearchableResource::make(User::query())->tap(new UserSearchable);

public function getOptions(): array
{
    return [
        'role' => [
            'admin',
            'customer'
        ]
    ];
}

public function getOptions(): array
{
    return [
        $this->field => [
            [
                'label' => 'Admin',
                'value' => 'admin'
            ],
            [
                'label' => 'Customer',
                'value' => 'customer'
            ]
        ]
    ];
}