Laravel 5.4:加载缓慢

时间:2018-04-03 08:42:23

标签: php laravel laravel-5 optimization eager-loading

我有慢负荷问题:差不多10秒。我想我必须以某种方式优化代码,但我不知道如何。有帮助吗?感谢。

这是控制器。我使用特征ListNoticias(在控制器下面)。

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

这是特质。在这里,我收集了每种语言的三个不同阵列中的所有新闻列表,然后我手动对它们进行分页。

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;

}
}

以更多信息编辑

根据评论的建议,我在下面的视图中找到了一个主要问题,我在其中调用每个新闻项目的图像,以便在列表中显示它以及url和slug。我已经在另一个内部创建了一个foreach。没有嵌套foreachs有没有办法做到这一点?谢谢。

部分视图包含图片和网址和slug:

@foreach($new->langs as $new_lang)
  @if($new_lang->lang_id == $lang_id)

    @foreach($data['images'] as $image)
        @if($image->imageable_id == $new->id && $image->main == '1')

            @php
                $mini = substr($image->path, 0, strrpos( $image->path, "/"));
                $name = substr($image->path, strrpos($image->path, '/') + 1);
                $image_mini = $mini.'/mini-'.$name;
            @endphp

            <div class="crop">
                <a href="{{ route('noticia', [$new->id, $new_lang->slug]) }}">
                    {{ HTML::image(file_exists($image_mini)? $image_mini : $image->path, '', array('class' => 'img-responsive ancho_100')) }}
                </a>
            </div>

        @endif
    @endforeach

  @endif
@endforeach

0 个答案:

没有答案