PHP code example of safemood / laravel-magic-scopes
1. Go to this page and download the library: Download safemood/laravel-magic-scopes 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/ */
Post::reviewedAt('2024-05-10')->get(); // Equivalent to: whereDate('reviewed_at', '2024-05-10')
Post::reviewedBefore('2024-05-15')->get(); // Equivalent to: whereDate('reviewed_at', '<', '2024-05-15')
Post::reviewedAfter('2024-05-15')->get(); // Equivalent to: whereDate('reviewed_at', '>', '2024-05-15')
Post::reviewedBetween('2024-05-10', '2024-05-20')->get(); // Equivalent to: whereBetween('reviewed_at', ['2024-05-10', '2024-05-20'])
declare(strict_types=1);
namespace Safemood\MagicScopes\Resolvers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Safemood\MagicScopes\Contracts\ScopeResolverContract;
class CustomScopeResolver implements ScopeResolverContract
{
public function matches(string $method, Builder $builder): bool
{
return Str::startWith($method, 'customPrefix');
}
public function apply(Builder $query, string $method, array $parameters, Model $model): Builder
{
$column = 'your_column_name'; // extract from method name
$value = $parameters[0] ?? null; // get the value, depending on your logic
return $builder->where($column, $value); // Apply basic WHERE condition
}
}