laravel-repositories
laravel-repositories copied to clipboard
Add support for query joins & cache results
Hi Thanks for your amazing product.
I found one issue when using the join method, the cache function doesn't work. Here is my code:
$query = self::where('status',1)->where('stock','>',0);
if($category)
$query = $query->where('category_id',$category);
$query = $query->where('name','like',"%$q%")->where('description','like',"%$q%",'or');
$query = $query->join('shops','shops.id','=','products.shop_id');
$query = $query->orderBy('total_sold','desc')->orderBy('updated_at','desc')
->paginate($num,['products.*','shops.name as shop_name']);`
The query is work and showing some result, but not the cache.
Is there any ide how to fix it?
Thanks Hendra1
I found the answer I add new method to BasicRepository
`
/**
* The query where clauses.
*
* @var array
*/
protected $join = [];
/**
* Add basic join to the query
*
* @param $foreignTable
* @param string $operator
* @param $primaryKey
* @param $foreignKey
* @return $this
*/
public function join($foreignTable, $operator = '=', $primaryKey, $foreignKey)
{
$this->join[] = [$foreignTable, $operator, $primaryKey, $foreignKey];
return $this;
}
/**
* Prepare query.
*
* @param object $model
*
* @return object
*/
protected function prepareQuery($model)
{
.........
// Add a basic join clause to the query
foreach ($this->join as $join) {
list($foreignTable, $operator, $primaryKey, $foreignKey) = array_pad($join, 4, null);
$model = $model->join($foreignTable, $operator, $primaryKey, $foreignKey);
}
`
Hope it will help other and Please add new method join in the master branch code
Thanks
@hendra1 I'll be happy to accept a PR for this feature 😉