I think you're looking for Global Scope http://laravel.com/docs/4.2/eloquent#global-scopes
Shortening Eloquent Call
I'm playing around with a learning project. I have companies, and users. Each company has many users. Every other table in my project is tied to a specific company (ex. company->projects, company->posts, etc.)
I find myself making the same Auth::user()->company call in every single controller.
Example:
Auth::user()->company->projects;
or
Auth::user()->company->posts;
Is there a way to globally set it, so that when I type Project::all(); it's always only projects for the logged in users company... or is there a better way to go about what I am trying to do?
Yes it messed so much with my head I deleted my post lol.
I don't think a scope is a good solution here why not just :
abstract class Controller extends BaseController {
public $category;
public function __construct()
{
$this->category = Auth::user()->category;
}
}
It is hard to shorten $this->category. And I think it is clearer than scoping every models.
Or
public function apply(Builder $builder, Model $model)
{
$builder->where('company_id', Auth::user()->company->id);
}
Or create a scope fromCompany for every models (to manage both hasMany and belongsToMany kind of relationship with company) and call it in the scope.
But Im sure scoping all your models like this will bite your neck somehow.
Please or to participate in this conversation.