Perhaps a first consideration may be, no matter how this is accessed, a cache MAY be applicable dependent on frequency of this needing to be resolved per user over a period.
Another consideration may be (and others may strongly disagree here) to create a stored function/procedure/etc. for accessing this info. I can think of a number of ways this can be done in PostgreSQL, which is what I am most familiar with.
Finally, to answer your question directly, may be to create models and requisite relationships for the different entities you are joining on and let the ORM do your work for you. I've noticed the queries created by Builder are quite decent and may save you from some headache further on in development. Eloquent also has the 'HasManyThrough' relationship, which may save you from having to write the join statements yourself.
If you would like me to expand on any aforementioned ideas or help construct the models, I will be happy to do so.
Something similir I have done, here you have (I am passing to repositories) but you can see some commented old code, using remember() to cache. That way I don't have to access database so many times. For example, module can be retrieved from a array, or you can cache it for a long timE ( ->FOREVER() ) and if you updated or insert some new just forget cache for that key. Just ideas. And the code bellow I tell you again, is not definitive, I have a definitive one but I didn't use repositories so I decided to rewrite it.
namespace Nireapp\Permissions;
use Nireapp\Permissions\PermissionInterface;
use \Auth;
use \App;
class PermissionBase implements PermissionInterface {
private $moduleToCheck;
private $errors;
public function __construct($module)
{
$this->moduleToCheck = $module ;
$this->userRepo = App::make('Nireapp\Repositories\Interfaces\UserRepositoryInterface');
$this->actionRepo = App::make('Nireapp\Repositories\Interfaces\ActionRepositoryInterface');
$this->moduleRepo = App::make('Nireapp\Repositories\Interfaces\ModuleRepositoryInterface');
$this->permissionRepo = App::make('Nireapp\Repositories\Interfaces\PermissionRepositoryInterface');
}
function can($actionName,$role_id=null)
{
if(is_null($role_id)){
$rol_id = Auth::user()->role_id;
}
$moduleTo = $this->$moduleToCheck;
$action_id = $this->actionRepo->getIdByName($actionName);
//::where('izena','=',$funtzio)->remember(120)->first()->id;
$module_id = $this->moduleRepo->getIdByName($moduleTo);
/* \Modulu::where('izena','=',$modulua)->where('mota','=','core')->remember(120)->first()->id;
\Arruntabaimen::where('rol_id','=',$id)->where('funtzio_id','=',$funtzio_id)
->where('modulu_id','=',$modulu_id)->remember(1)->exists()
*/
if( $this->permissionRepo->existsPermission($role_id,$action_id,$module_id) || $this->userRepo->isAdmin() )
{
return true;
}else{
$this->errors = 'ez duzu baimenik "'.$moduleTo.'" moduluan "'.$actionName.'" funtzioa burutzeko';
return false;
}
}
function getErrors()
{
return $this->errors;
}
function changeModule($moduleName)
{
$this->moduleToCheck = $moduleName;
return $this;
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community