Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Paginacja dla elementów znajdujących się w relacji w Laravelu
trifek
post 12.12.2019, 09:37:25
Post #1





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


Witajcie.

Mam taki kod:

  1. class ForumCategory extends Model
  2. {
  3. use scopeActiveTrait;
  4.  
  5. protected $quarded = ['id'];
  6. protected $fillable = ['company_id', 'enable', 'name', 'url_address', 'enable'];
  7. public $timestamps = false;
  8.  
  9. public function themes()
  10. {
  11. return $this->hasMany('App\ForumPost', 'id_category', 'id')->where('parent_id', '=', null);
  12. }
  13.  
  14. public function lastPost()
  15. {
  16. return $this->themes()->orderBy('id', 'desc')->first();
  17. }
  18. }
  19.  
  20. class ForumPost extends Model
  21. {
  22. use scopeActiveTrait;
  23.  
  24. protected $quarded = ['id'];
  25. protected $fillable = ['company_id', 'id_category', 'parent_id', 'user_id', 'date', 'title', 'content', 'url_address', 'enable'];
  26. public $timestamps = true;
  27. protected $table = 'forum';
  28.  
  29. public function category()
  30. {
  31. return $this->belongsTo('App\ForumCategory', 'id_category');
  32. }
  33.  
  34. public function author()
  35. {
  36. return $this->belongsTo('App\User', 'user_id');
  37. }
  38.  
  39. public function postCount()
  40. {
  41. return $this->hasMany('App\ForumPost', 'parent_id', 'id')->where('parent_id', '!=', null)->count();
  42. }
  43. }



Po wywołaniu funkcji:

  1. ForumCategory::with('themes')->orderBy('name', 'asc')->paginate(35);



Mam paginację dla kategorii.


W momencie gdy wyświetlam wyniki tej funkcji:

  1. @foreach ($forums as $forum)
  2. Welcome in {{ $forum->name }}<br/>
  3. @foreach ($forum->themes as $post)
  4. Post: {{ $post->title }}
  5. @endforeach
  6. <div class="paginationFormat"></div>
  7. <div class="text-right">{{ $forums->links() }}</div>
  8. @endforeach



Mogę wyświetlić paginację dla $forums.

Chciałbym dodatkowo wyświetlić paginację dla $post (danych z relacji). W jaki sposób można to zrobić?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 23.04.2024 - 09:13