jeudi 29 septembre 2016

Prevent lazy loading in view

I have a JSON result from a query that looks like this:

{  
    "id":1,
    "user_id":"1",
    "message":"Hello, world",
    "created_at":"2016-09-22 00:32:20",
    "updated_at":"2016-09-22 00:32:20",
    "stats": [
        ...
    ]
},
{  
    "id":2,
    "user_id":"1",
    "message":"Hello, world",
    "created_at":"2016-09-22 00:32:20",
    "updated_at":"2016-09-22 00:32:20",
},
{
    ... more results
}

Notice that sometimes the result has a stats property and sometimes it does not (despite every record having a stats relationship). Don't ask why, that's just how I have it set up in the backend.

I want to loop through these results in my view, like this:

@foreach ($posts as $post)
    @if (isset($post->stats) && !empty($post->stats)
        
    @endif
@endforeach

However, for post id 2, the loop will also output the $post->stats->total value because it lazy loads the stats.

How can I prevent it from lazy loading the stats relationship?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire