Support the ongoing development of Laravel.io →
posted 11 years ago
Cache

I am using the file cache for an in office application which has about 5 users. The cache is working fine for put and get, I am using remember to store queries. The problem is with the format of the data. The queries have joins, I am using eloquent with eager loading.

On the view, if I am using the data from the cache I cannot you the adjoining data.

I get the following error

Undefined property: stdClass::$account (View: C:\wamp\www\KSS-CRM\app\views\leads\search.blade.php)

Only part of the code is below.

The view file is

<div class="header">Possible Contacts</div>
@if (count($contacts))
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Client</th>
                <th>Phone</th>
                <th>Mobile Phone</th>
                <th>Email</th>
                <th>Description</th>
            </tr>
        </thead>

        <tbody>
            @foreach ($contacts as $contact)
                <tr>
                    <td>{{ link_to_route('contacts.show', $contact->FirstName, array($contact->id)) }}</td>
                    <td>{{{ $contact->LastName }}}</td>
                    <td>{{{ is_object($contact->account) ? $contact->account->Name : '' }}}</td>
                    <td>{{{ $contact->Phone }}}</td>
                    <td>{{{ $contact->MobilePhone }}}</td>
                    <td>{{{ $contact->Email }}}</td>
                    <td>{{{ $contact->Description }}}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
@else
    <div class="info">There are no possible contacts</div>
@endif

The model is

public function account()
    {
        return $this->hasOne('Account', 'id', 'AccountId');
    }

The controller is

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

Any help would be grateful.

Last updated 3 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 3 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 3 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 3 years ago.
0

Sign in to participate in this thread!

PHPverse

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.

© 2025 Laravel.io - All rights reserved.