php - Laravel Stuck getting last user who posted in forum -


so have basic forum set , have relationships between models completed. stuck trying last user posted in sub category. how laid out.

mysql tables forum_category (this used main category , sub category) forum_post (this used posts)

here forumcategory.php

namespace app\models\forum; use illuminate\database\eloquent\model;  class forumcategory extends model {      protected $table = 'forum_category';      public static function category()     {         return forumcategory::with('forumsubcategory')->wherenull('parent_id')->get();     }     public function user()     {         return $this->belongsto('app\user');     }     public function forumsubcategory()     {         return $this->hasmany('app\models\forum\forumsubcategory' , 'parent_id');     }     public function forumpost()     {         return $this->hasmany('app\models\forum\forumpost' , 'parent_id');     } } 

and here forumsubcategory.php sub category model though using same table...

namespace app\models\forum;  use app\user; use illuminate\database\eloquent\model;  class forumsubcategory extends model {     protected $table = 'forum_category';      public function forumcategory()     {         return $this->belongsto('app\models\forum\forumcategory');     }     public function user()     {       return $this->belongsto('app\user');     }     public function forumpost()     {         return $this->hasmany('app\models\forum\forumpost' , 'parent_id');     } } 

and here forumpost.php model

namespace app\models\forum;  use illuminate\database\eloquent\model;  class forumpost extends model {     protected $table = 'forum_post';      public function user()     {       return $this->belongsto('app\user');     }     public function forumsubcategory()     {         return $this->belongsto('app\models\forum\forumsubcategory');     } } 

the controller pretty basic, index

public function index() {    return view('forum.index', ['category' => forumcategory::category()]); } 

now @ moment getting last user posted this

public static function lastpost($value) {     $lastpost = forumpost::whereparentid($value)->orderby('created_at', 'desc')->pluck('user_id')->first();     return user::whereid($lastpost)->pluck('username')->first(); } 

which not want do. want know correct way of doing :(

here view

<ul>     @foreach ($category $cat)     <li>         {{$cat->category_name}} - main category         {{$cat->category_description}} - main category description          @if ($cat->forumsubcategory)         <ul>             @foreach ($cat->forumsubcategory $sub)                 <li>                     <a href="/forum/{{$cat->slug}}/{{$sub->slug}}">  - category slug sub category slug                         {{$sub->category_name}} - sub category name                     </a>                     {{$sub->category_description}} - sub category description                     {{$sub->forumpost->count()}} - how many posts in sub category                     {{$sub->lastpost($sub->id)}} - how getting last user posted                 </li>             @endforeach         </ul>         @endif     </li>     @endforeach </ul> 

thanks taking time @ this!

the fastest solution:

you can filter/order relationships, hence function below.

latest() eloquent shortcut orderby('created_at','desc')

forumsubcategory model:

class forumsubcategory extends model {     ...     public function latestpost()     {         return $this->hasone('app\models\forum\forumpost' , 'parent_id')->latest()->with('user');     } } 

in view:

from:

{{$sub->lastpost($sub->id)}} - how getting last user posted 

to:

@if($sub->latestpost)      @if($sub->latestpost->user)         {{$sub->latestpost->user->username or 'username not set'}}     @else        {{'user not found'}}     @endif @else     {{'no last post'}} @endif 

the stable solution should service class around forum sub categories take care of edge cases such users being deleted, posts banned , forth.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -