Support the ongoing development of Laravel.io →
posted 10 years ago
Cache
Last updated 2 years ago.
0

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();
Last updated 2 years ago.
0

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');
}

Last updated 2 years ago.
0

Thank you, that solved the problem. It is my first time using cache it is is simpler than I expected.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.