【问题标题】:Sort instances of a loop in Laravel在 Laravel 中对循环的实例进行排序
【发布时间】:2022-01-25 00:10:25
【问题描述】:

您好,过去几周我一直在学习 Laravel。虽然我遇到了根据 this 的值对这个循环的每个实例进行排序的问题 ----> $post->user->receivedUpvotes->count() - $post->user->receivedDownvotes- >count()

@foreach($posts->unique('user_id') as $post)   
  <a href="{{ route('users.posts', $post->user) }}" class="font-bold text-xl">
    {{ $post->user->name }}
  </a>
  <p class="mb-4">
    <span class="mr-3 text-gray-600">Karma Gained: </span>
    <span class="text-green-700 font-semibold">
      {{ $post->user->receivedUpvotes->count() - $post->user->receivedDownvotes->count() }}
    </span>
  </p> 
@endforeach

我有点坚持寻找对它们进行排序的解决方案,因为它们的值不在数据库中

我的模型是这样的

User.php

public function posts()
{
    return $this->hasMany(Post::class);
}

public function upvotes()
{
    return $this->hasMany(Upvote::class);
}

public function receivedUpvotes()
{
    return $this->hasManyThrough(Upvote::class, Post::class);
}

public function downvotes()
{
    return $this->hasMany(Downvote::class);
}

public function receivedDownvotes()
{
    return $this->hasManyThrough(Downvote::class, Post::class);
}

Post.php

public function upvotedBy(User $user)
{
    return $this->upvotes->contains('user_id', $user->id);
}

public function downvotedBy(User $user)
{
    return $this->downvotes->contains('user_id', $user->id);
}

public function user()
{
    return $this->belongsTo(User::class);
}

public function upvotes()
{
    return $this->hasMany(Upvote::class);
}

public function downvotes()
{
    return $this->hasMany(Downvote::class);
}

Upvote.php 和 Downvote.php 都有这个

use HasFactory, SoftDeletes;

    protected $fillable = [
        'user_id'
    ];

这是我的控制器DashboardController.php,目前我只放它来显示最新的帖子

class DashboardController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth']);
    }

    public function index(Post $post)
    {
        $posts=Post::latest()->with(['user','upvotes'])->get();
        return view ('dashboard',['posts'=>$posts]);    
    }        
}

【问题讨论】:

  • 你能不能尝试在User.php中添加一个函数``` public function getNetVotesAttribute() { return $this->receivedUpvotes()->count() - $this->receivedDownvotes()- >计数(); } ```
  • 对不起,刚才不小心点了回车,参考我上面编辑的评论,然后你可以按功能对用户进行排序,或者如果你在User.php中的$appends数组中添加“netVotes”,你可以直接使用 netVotes 排序。例如$posts = Post::all()-&gt;sortBy(function($post) { return $post-&gt;user-&gt;netVotes; }); 或者 f 你没有添加到 $appends 数组中,$posts = Post::all()-&gt;sortBy(function($post) { return $post-&gt;user-&gt;getNetVotesAttribute(); });
  • 看来它成功了!非常感谢您的帮助,我了解了函数背后的思想并按降序排序!
  • 很高兴它有帮助!也许我也会添加作为将来参考的答案

标签: laravel laravel-blade


【解决方案1】:

在User.php中,添加

public function getNetVotesAttribute() 
{
    return $this->receivedUpvotes()->count() - $this->receivedDownvotes()->count(); 
} 

然后您可以使用该函数对您的用户进行排序,或者如果您将“netVotes”添加到User.php 中的$appends 数组中,您可以直接使用netVotes 进行排序。例如

$posts = Post::all()->sortBy(function($post) { 
    return $post->user->netVotes; 
}); 

或者如果您没有添加到 $appends 数组中,

$posts = Post::all()->sortBy(function($post) { 
    return $post->user->getNetVotesAttribute(); 
}); 

【讨论】:

    猜你喜欢
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多