如何设置“最近新闻”帖子的限制?我想设置4的限制,但它显示了所有这些限制。作为奖励,最后插入一个“查看所有新闻”按钮......
我有这个:
@foreach($news as $news)
@php
$news_img = ($news['anexo'] ? get_image_small_url($news['anexo']['ane_arquivo']) : '');
$news_img = $news_img ?: 'http://placehold.it/300.jpg?text=%20';
$news_txt = (empty($news['pub_resumo']) ? $news['pub_conteudo'] : $news['pub_resumo']);
@endphp
<div class="media media-thumb">
@if($news_img)
<div class="media-left">
<a href="{{ url('/news/'.$news['post_id']) }}">
<img class="media-object" src="{{ $news_img }}" alt="{{ $news['pub_titulo'] }}">
</a>
</div>
@endif
<div class="media-body">
<div class="post-meta">
<span class="published">{{ date('d/m/Y', strtotime($news['pub_data_cadastro'])) }}</span>
@if(!empty($news['categories']))
<span class="categories">{{ join(', ', array_column($news['categories'], 'tcp_nome')) }}</span>
@endif
</div>
<h4 class="media-heading">
<a href="{{ url('/news/'.$news['post_id']) }}">{{ $news['title'] }}</a>
</h4>
<p>{{ get_excerpt($news_txt, 180) }}</p>
</div>
</div>
@endforeach
控制器
$news = Publicacao::select('publicacao_id', 'pub_titulo', 'pub_conteudo', 'pub_resumo', 'pub_data_cadastro', 'anexo_id')
->with(['categorias', 'anexo'])
->where('pub_tipo', 'NOT')
->where('pub_status', 'PUB')
->limit(5)
->orderBy('pub_data_cadastro', 'desc')
->get();
$this->data['news'] = $news->toArray();
return view('site.home', $this->data);
}
干杯!