lundi 1 février 2016

laravel 5.1 using Lazy Eager Loading usage

I'm working on a support ticket tool. Table design at the moment:

tickets: |id|supp_id|title|user_id|...

ticket_replies: |id|ticket_id|user_id|text

files: |id|ticket_replie_id|name

model of ticket

public function ticket_replie() 
{
    return $this->hasMany('App\ticket_replie', 'ticket_id', 'id');
}

model of ticket_replie

public function file() 
{
        return $this->hasOne('App\File', 'ticket_replie_id', 'id');
}

controller

$ticket = Auth::user()->tickets()->where('id', $id)->firstOrFail();
   return view('protected.ticketDetail', compact('ticket'));

view

    ID: {{$ticket->id}}
    title: {{ $ticket->title}}<br>
    status: {{ returnStatus($ticket->status) }}<br>
    Ticket created: {{ $ticket->created_at }}<br>


    @if (!$ticket->supporter)
        supporter:-<br></br></br>
    @else
        supporter  {{ $ticket->supporter->username }}<br></br>
    @endif

    @foreach($ticket->ticket_replie as $reply)
        @if ($reply->file == null)
            reply text: {{ $reply->text }}</br>
        @else
            reply text: {{ $reply->text }}</br>
            file: <a href="/path/to/file/{!! $reply->file->name !!}">Download file</a><br>
        @endif
    reply created at: {{$reply->created_at}}</br></br>
    @endforeach 

current screen of querys: querys debug

Each ticket_replie can contain exact one "file", which stands for an attached file. As you may see in the querys this generates much load. Is there an way to use (laravel Lazy Eager Loading) to minimize amount of querys?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire