I would not suggest using ->visible(...) becuse that only hides field using CSS, data is still there and that is the problem.
@p3drojimenez I've just delth with same thing over last 2 days by setting different scopes and removing scopes. Example:
Assuming you have set globalScope in your booted with name "allowed"
use Illuminate\Database\Eloquent\Builder;
// in your Model class
class MySimpleTestModel extends Model
{
protected static function booted()
{
/**
* Adds a global scope to filter the query by the authenticated user's company ID.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return void
*/
static::addGlobalScope('allowed', function ($query) {
// Just an example of how to use for all users as general scope
$query->where('allowed_to_see_me', !auth()->user()->is_admin)
->where('is_from_web', true);
});
// You can specify as many parameters as you need or NONE if you don't need it (assuming you will need)
public function scopeOnlyAdmin(Builder $query, string $your_filter = null, $limit = 10)
{
// just admins
$query->where('allowed_to_see_me', auth()->user()->is_admin);
// build your query as needed ->where(.....);
}
}
}
Then in your Resource you can exclude scope where you need to show something to admin in your query by using:
Keep note on this: ofOnlyAdmin starts with "of" where scope function name is "scopeOnlyAdmin". As well on removing scope "withoutGlobalScope" in example below.
// MySimpleTestModel::ofOnlyAdmin() if you don't have any extra parameters
$products = MySimpleTestModel::ofOnlyAdmin($filter, $limit)->withoutGlobalScope('allowed')
->get()
->toArray();
You can extend logic and add multiple scopes and exclude multiple of them in chain if needed. I hope this helps.
Resource: https://laravel.com/docs/10.x/eloquent#removing-global-scopes