1
我想列出我的帖子個月Laravel - 通過幾個月組帖子(預先加載)
分組這是我的分類模型
class Category extends \Eloquent {
public function posts()
{
return $this->belongsToMany('Post', 'category_post_tag')
->select(DB::raw('MONTH(posts.eventStartDate) month, MONTHNAME(posts.created_at) month_name'))
->distinct()
->orderBy('eventStartDate', 'asc');
}
}
所以,當我在查看文件做到這一點:
@foreach($category->posts as $index => $post)
<pre>{{ $post }}</pre>
@endforeach
我得到這樣的結果:
{"month":"7","month_name":"July","id":"20","slug":"sunt-et-ipsum-dolorum","title":"Sint dolorum exercitationem."}
{"month":"7","month_name":"July","id":"15","slug":"qui-exercitationem-autem","title":"Id tenetur rem cumque ut."}
{"month":"8","month_name":"July","id":"6","slug":"aspernatur-qui-repellat","title":"Recusandae accusamus omnis dicta."}
{"month":"9","month_name":"July","id":"25","slug":"et-aut-eos-quaerat-soluta-perspiciatis","title":"Facere ullam sint et."}
{"month":"10","month_name":"July","id":"26","slug":"sunt-nemo-aperiam","title":"Voluptatibus harum sunt et ducimus."}
這是好的,因爲我只有5個職位,但我想以某種方式分組在同一個月的職位,所以我可以列出他們在我看來分組月在我的分類模型,但那個「月」),以我的帖子的方法(關係)只能使事情變得更糟,因爲那時我只拿回一個職位/月
所以我在我的視圖文件試過這樣:
@foreach($category->posts as $index => $post)
{{ $new_array[$post->month][$post->id] = $post }}
@endforeach
創建一個按月份分組的新陣列,但是這會凍結我的瀏覽器,它只是繼續加載並最終死於白屏。
有人請指點我正確的方向,非常感謝。
那不是工作,它只是不斷載荷和載荷,什麼都不會發生 –
@ Mr.Sam - 有多少職位有哪些? –
只有5個,但我用不同的方式解決了它,我明天會發布答案,今晚會很累。無論如何謝謝@Josep –