PHP code example of goodcat / laravel-querystring

1. Go to this page and download the library: Download goodcat/laravel-querystring 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/ */

    

goodcat / laravel-querystring example snippets


   use Illuminate\Database\Eloquent\Builder;
   use Goodcat\QueryString\Traits\UseQueryString;
   use Goodcat\QueryString\Attributes\QueryString;
   
   class User extends Authenticatable
   {
       use UseQueryString;
       
       #[QueryString('email')]
       public function filterByEmail(Builder $query, string $search): void
       {
           $query->where('email', $search);
       }
   }
   

   class UserController extends Controller
   {
       public function index(Request $request): View
       {
           // E.g. https://example.com/[email protected]
           $users = User::query()->queryString($request)->get();
   
           return view('user.index', ['users' => $users]);
       }
   }
   

#[QueryString('name')]
public function filterByName(Builder $query, string $search): void 

#[QueryString('name')]
#[QueryString('email')]
public function genericStringSearch(Build $query, string $search, string $name): void
{
    $query->where($name, 'like', "$search%");
}

public function index(Request $request): View
{
    $filters = $request->query();
    
    // Change $filters array as desired.
    
    $users = User::query()->queryString($filters)->get();
    
    return view('user.index', ['users' => $users]);
}

   class User extends Authenticatable
   {
       use UseQueryString;

        protected function getQueryStringObject(): object
        {
            return new CustomFilterClass();
        }
   }
sh
php artisan vendor:publish --provider="Goodcat\QueryString\QueryStringServiceProvider"