Laravel Stuck获取在论坛发表的最新用户

时间:2016-04-19 08:17:00

标签: php mysql laravel

所以我有一个基本的论坛设置,我已经完成了模型之间的关系。但是,我试图让最后一个用户在子类别中发布。这就是它的布局。

mysql表 forum_category(这用于主类别和子类别) forum_post(这用于帖子)

这是我的 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');
    }
}

这是我的 ForumSubCategory.php 子类别模型,即使他们使用相同的表...

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

这是我的 ForumPost.php 模型

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

控制器非常基本,索引是

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

现在,目前我正在收到上次发布此用户的用户

public static function LastPost($value)
{
    $lastPost = ForumPost::whereParentId($value)->orderBy('created_at', 'DESC')->pluck('user_id')->first();
    return User::whereId($lastPost)->pluck('username')->first();
}

我不想做。我想知道这样做的正确方法:(

这是我的观点

<ul>
    @foreach ($category as $cat)
    <li>
        {{$cat->category_name}} - this is main category
        {{$cat->category_description}} - this is main category description

        @if ($cat->ForumSubCategory)
        <ul>
            @foreach ($cat->ForumSubCategory as $sub)
                <li>
                    <a href="/forum/{{$cat->slug}}/{{$sub->slug}}">  - this is the category slug with the sub category slug
                        {{$sub->category_name}} - this is the sub category name
                    </a>
                    {{$sub->category_description}} - this is the sub category description
                    {{$sub->ForumPost->count()}} - this is how many posts are in the sub category
                    {{$sub->LastPost($sub->id)}} - this is how i am currently getting the last user who posted
                </li>
            @endforeach
        </ul>
        @endif
    </li>
    @endforeach
</ul>

感谢您花时间看看这个!

2 个答案:

答案 0 :(得分:1)

最快的解决方案:

您可以过滤/订购关系,因此可以使用以下功能。

latest()orderBy('created_at','DESC')

的雄辩捷径

ForumSubCategory模型:

class ForumSubCategory extends Model
{
    ...
    public function latestPost()
    {
        return $this->hasOne('App\Models\Forum\ForumPost' , 'parent_id')->latest()->with('user');
    }
}

在您看来:

自:

{{$sub->LastPost($sub->id)}} - this is how i am currently getting the last user who posted

要:

@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

最稳定的解决方案应该是围绕论坛子类别的服务类,以处理边缘情况,例如用户被删除,帖子被禁止等等。

答案 1 :(得分:0)

您可以使用动态属性。查看Relationship Methods Vs Dynamic Properties

// Controller
public function lastPost()
{
    $lastPost = ForumPost::whereParentId($value)->orderBy('created_at', 'DESC')->first();

    $username = $lastPost->user->username;

    return view('post.view', compact('username'));
}