PHP code example of dangerwayne / laravel-specifications
1. Go to this page and download the library: Download dangerwayne/laravel-specifications 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/ */
dangerwayne / laravel-specifications example snippets
use DangerWayne\Specification\Specifications\Common\WhereSpecification;
$activeUsers = User::query()
->whereSpecification(new WhereSpecification('status', 'active'))
->get();
use DangerWayne\Specification\Specifications\AbstractSpecification;
use Illuminate\Database\Eloquent\Builder;
class PremiumUserSpecification extends AbstractSpecification
{
public function isSatisfiedBy(mixed $candidate): bool
{
return $candidate->subscription === 'premium';
}
public function toQuery(Builder $query): Builder
{
return $query->where('subscription', 'premium');
}
}
new WhereSpecification('status', '=', 'active');
new WhereSpecification('age', '>', 18);
new WhereSpecification('name', 'like', '%john%');
new WhereInSpecification('status', ['active', 'pending']);
new WhereBetweenSpecification('age', 18, 65);
new WhereNullSpecification('email_verified_at');
new WhereHasSpecification('posts', new WhereSpecification('published', true));
Specification::create()
->where('field', 'operator', 'value') // Basic where clause
->where('field', 'value') // Defaults to '=' operator
->whereIn('field', [1, 2, 3]) // WHERE IN clause
->whereBetween('field', 1, 10) // BETWEEN clause
->whereNull('field') // IS NULL clause
->whereNotNull('field') // IS NOT NULL clause
->whereHas('relation', $specification) // Has relationship
->or() // Next condition uses OR
->build(); // Build the specification