mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

Cached queries with different select fields statement #238

Closed mrahmadt closed 4 years ago

mrahmadt commented 5 years ago

Describe the bug Query database with deferent fields (and same "where" statement) will return the first cached result without respecting the requested/selected fields

Eloquent Query

public function getChildren(Request $request){ //---> return all fields
return Child::where(['user_id'=>$request->auth->id])->get();
}

public function getNames(Request $request){ //---> Should return only "id" & "name"
return Child::where(['user_id'=>$request->auth->id])->select('id','name')->get();
}

Environment

jcsoriano commented 5 years ago

The interesting thing is specifying the columns within the ->get('...') method works, but specifying it through ->select('...') doesn't. So as a temporary workaround, maybe you should put your columns in ->get() instead of ->select(), like below:

Child::where(['user_id' => $request->auth->id])->get(['id', 'name']);

Environment

mrahmadt commented 5 years ago

Excellent.

Thanks, this is working fine when I put columns in get().

mikebronner commented 5 years ago

Keeping this issue open to investigate the select() issue.

mikebronner commented 5 years ago

This is indeed a bug, will need to ponder on this a while to see how to implement this. Will welcome any PRs for this that satisfy the test testSelectFieldsAreCached() (current commented out). While I have the cache key differentiating, the results still contain all columns. Intercepting the select clause is a challenge.

dmason30 commented 4 years ago

@mikebronner I think this one can be closed the test seems to pass?

mikebronner commented 4 years ago

@dmason30 Thanks for checking on this! :)