For queries being cached, you dont have to get the results from the cache again with get. All you have to do is run the same query.
put this
if(!Cache::has('contacts'.$search)){
$contacts = Contact::with('account')
->where(function ($sql) use ($search)
{
$sql->where('FirstName', 'LIKE', '%'.$search.'%');
$sql->orWhere('LastName', 'LIKE', '%'.$search.'%');
$sql->orWhere('Description', 'LIKE', '%'.$search.'%');
})
->where('IsDeleted', '=', '0')
->orderBy('LastModifiedDate', 'DESC')
->remember($minutes, 'contacts'.$search)
->get();
}else{
$contacts = Cache::get('contacts'.$search);
}
to
$contacts = Contact::with('account')
->where(function ($sql) use ($search)
{
$sql->where('FirstName', 'LIKE', '%'.$search.'%');
$sql->orWhere('LastName', 'LIKE', '%'.$search.'%');
$sql->orWhere('Description', 'LIKE', '%'.$search.'%');
})
->where('IsDeleted', '=', '0')
->orderBy('LastModifiedDate', 'DESC')
->remember($minutes, 'contacts'.$search)
->get();
There's a problem with that relation:
// wrong
public function account()
{
return $this->hasOne('Account', 'id', 'AccountId'); // keys are swapped
}
// change it to:
public function account()
{
return $this->hasOne('Account', 'AccountId', 'id');
}
Thank you, that solved the problem. It is my first time using cache it is is simpler than I expected.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community