0

i want to overwrite the get function in my Customer Model.

 public function get(array $options = [])
 {
   $user = \Auth::user();

   if($user->role_id == 1){
     return parent::get();

   }else if($user->role_id == 4){
     return parent::where("user_id", $user->id)->get();

   }else if($user->role_id == 3){
     $users = User::where("user_id", $user->id)->get();

     return parent::where("user_id", $user->id)->orWhereIn('user_id', $users)->get();
   }
  }

Is this possible ?

1
  • 1
    What do you mean? The get function basically runs the query. Are you wanting to manipulate the output? Please provide a bit more info. Commented Jan 9, 2019 at 15:18

1 Answer 1

1

What you need is global scopes

Global scopes allow you to add constraints to all queries for a given model. Laravel's own soft delete functionality utilizes global scopes to only pull "non-deleted" models from the database. Writing your own global scopes can provide a convenient, easy way to make sure every query for a given model receives certain constraints.

https://laravel.com/docs/5.7/eloquent#query-scopes

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your answer. i think it could work with scopes but now when i try to add a whereIn Query i get this error $builder->where("user_id", $user->id)->orWhereIn('user_id', $users)->get(); Out of memory (allocated 16777216) (tried to allocate 917504 bytes) php.ini: memory_limit = 600M
Set toSql() insted of get() and dd() the result. It should put some light on whats going on with your query
@CemFlav, whereIn accepts an array of items, I think you need to pass an array of Ids instead of a collection of objects. Try plucking the Id from the users before passing the data to the whereIn function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.