1. Go to this page and download the library: Download sowailem/fieldguard 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/ */
use Sowailem\FieldGuard\Facades\FieldGuard;
public function view(User $user)
{
// Returns the model attributes as an array, filtered by security rules
return FieldGuard::apply($user, auth()->user());
}
public function toArray($request)
{
return FieldGuard::apply($this->resource, $request->user());
}
// Laravel 11+ example in bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'fieldguard' => \Sowailem\FieldGuard\Middleware\EnforceFieldSecurityMiddleware::class,
]);
})
namespace App\Security;
use Sowailem\FieldGuard\Contracts\PolicyResolver;
use Illuminate\Database\Eloquent\Model;
class MyCustomResolver implements PolicyResolver
{
public function resolve(array $policy, Model $model, $user): bool
{
// Your custom logic here
return true;
}
}