mardi 3 avril 2018

Laravel 5.4: slow loading

I have problem with slow load: almost 10 seconds. I guess I have to optimize the code in some way, but I don't know how. Any help? Thanks.

This is the controller. I use the trait ListNoticias (below the controller).

class NoticiaController extends Controller
{
use ListNoticias;

public function actualidad(Request $request)
{
    $data['section_id'] = explode(',', '1,2,3');

    $data['ruta'] = 'actualidad';
    $data['title'] = __('header.actualidad');
    $data['num'] = $request->num;
    $url = $request->url();

    $data = $this->listNoticias($data, $url);

    return view('web.actualidad.listado', compact('data'));
}
}

And this is the trait. Here, I collect a list of all the news in three different arrays for each of the languages and then I manually paginate them.

trait ListNoticias
{
public function listNoticias($data, $url)
{
    $now = date('Y-m-d');
    $time = date('H:i:s');

    (isset($data['num']))? $num = $data['num'] : $num = '15';

    $data['images'] = Image::where('imageable_type', 'App\Models\Noticia')->get();
    $data['sections']  = Section::all();

    $data['noticias'] = Noticia::where('date', '<', $now)
        ->where('active', '1')
        ->whereIn('section_id', $data['section_id'])
        ->orWhere('date', '=', $now)
        ->where('time', '<=', $time)
        ->where('active', '1')
        ->whereIn('section_id', $data['section_id'])
        ->orderBy('date', 'desc')
        ->orderBy('time', 'desc')
        ->get();

    $data['noticias-es'] = [];
    $data['noticias-en'] = [];
    $data['noticias-pt'] = [];

    foreach($data['noticias'] as $row){
        foreach($row->langs as $row_lang) {
            if ($row_lang->lang_id == '1') {
                $data['noticias-es'][] = $row;
            } elseif ($row_lang->lang_id == '2') {
                $data['noticias-en'][] = $row;
            } elseif ($row_lang->lang_id == '3') {
                $data['noticias-pt'][] = $row;
            } else null;

        }
    }

    // Manual paginate
    /*  Get current page form url e.x. &page=1
        Create a new Laravel collection from the array data
        Slice the collection to get the items to display in current page
        Create our paginator and pass it to the view
        set url path for generated links
    */

    $currentPage = LengthAwarePaginator::resolveCurrentPage();

    // ES
    $itemCollection = collect($data['noticias-es']);
    $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
    $data['noticias-es'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
    $data['noticias-es']->setPath($url);

    // EN
    $itemCollection = collect($data['noticias-en']);
    $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
    $data['noticias-en'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
    $data['noticias-en']->setPath($url);

    // PT
    $itemCollection = collect($data['noticias-pt']);
    $currentPageItems = $itemCollection->slice(($currentPage * $num) - $num, $num)->all();
    $data['noticias-pt'] = new LengthAwarePaginator($currentPageItems , count($itemCollection), $num);
    $data['noticias-pt']->setPath($url);

    return $data;

}
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire