Is there any reason for not simply returning Accounts::all() ?
Nov 10, 2014
6
Level 1
Eloquent model relationship, but without constraints?
This may seem like an odd request, but I can't think of any other way to put it.
My application has a many-to-many relationship between users and "accounts". A single account can have many users, and a single user can access multiple accounts.
So I have admin users in my app that should have access to all current and future accounts. My idea was to create an eloquent relationship that works like HasMany or BelongsTo, but without that where clause that limits results.
public function accounts()
{
if($this->hasRole(Role::ADMIN)) {
return $this->hasAll(Account::class);
}
return $this->belongsToMany(Account::class);
}
// That way, in the application...
$user->hasRole(Role::ADMIN); // => true
$user->accounts; // => all accounts in my database
$user->hasRole(Role::ADMIN); // => false
$user->accounts; // => only assigned accounts
Is something like this possible?
Please or to participate in this conversation.