Php 基于两个不同因素获取数据库中最新输入的算法
我有一个基于拉威尔的网站。网站由用户组成,用户可以相互关注<代码>用户可以发出<代码>叫喊(如推特),相互跟踪的用户应该能够看到这些。我的数据库中有一个Php 基于两个不同因素获取数据库中最新输入的算法,php,mysql,laravel,Php,Mysql,Laravel,我有一个基于拉威尔的网站。网站由用户组成,用户可以相互关注用户可以发出叫喊(如推特),相互跟踪的用户应该能够看到这些。我的数据库中有一个User表,一个Shoutout表和一个Follow表 $shoutouts = Shoutout::join('follow', 'shoutout.user_id', '=', 'follow.followed_user_id') ->where('follow.user_id', $user->id)
User
表,一个Shoutout
表和一个Follow
表
$shoutouts = Shoutout::join('follow', 'shoutout.user_id', '=', 'follow.followed_user_id')
->where('follow.user_id', $user->id)
->select(''shoutout.*'')
->orderBy('shoutout.created_at', 'desc')
->get()
由于它是由i Laravel生成的,用户与Shoutout和Follow table都有关系
因此,当用户登录时,我希望获得该用户正在跟踪的所有人。这是我通过以下方式做到的:
$following = Follow::whereUser_id($user->id)->get();
但是,我现在如何从$following
中的用户那里获得Shoutouts
,从创建shoutout时开始按降序排列,这样我就可以在视图中执行foreach($Shoutouts as$shoutout)?
其想法是,用户可以看到用户跟踪的人的最新叫喊声
如果我这样做:
$following = Follow::whereUser_id($user->id)->get();
foreach($following as $follow) {
$shoutouts = $shoutouts . Shoutout::whereUser_id($follow->user_id)->get();
}
我会以不正确的顺序获取它们,因为这会给我一个特定用户的所有喊叫声,但我希望以降序获取它们,因此用户会首先看到最新的喊叫声
感谢所有帮助。您正在跟踪用户的留言中寻找帮助。如果您正确定义了关系,您应该能够:
$following = Follow::whereUser_id($user->id)->with('shoutouts')->get();
然后通过以下方式访问用户的喊叫声:
foreach($following as $follow) {
$shoutouts = $follow->shoutouts;
//Do Something With The Collection Of Shoutouts
}
更新
为了获得他们关注的用户的叫喊声,您可以在用户表上定义一个hasManyThrough
:
public function followerShoutouts()
{
return $this->hasManyThrough(Shoutout::class, Follow::class, 'user_id', 'user_id_being_followed');
}
然后你可以做:
$shoutouts = User::with(['followerShoutouts' => function($query) {
return $query->orderBy('created_at', 'desc');
}])->find($userId);
您正在跟踪用户的叫喊声中查找。如果您正确定义了关系,您应该能够:
$following = Follow::whereUser_id($user->id)->with('shoutouts')->get();
然后通过以下方式访问用户的喊叫声:
foreach($following as $follow) {
$shoutouts = $follow->shoutouts;
//Do Something With The Collection Of Shoutouts
}
更新
为了获得他们关注的用户的叫喊声,您可以在用户表上定义一个hasManyThrough
:
public function followerShoutouts()
{
return $this->hasManyThrough(Shoutout::class, Follow::class, 'user_id', 'user_id_being_followed');
}
然后你可以做:
$shoutouts = User::with(['followerShoutouts' => function($query) {
return $query->orderBy('created_at', 'desc');
}])->find($userId);
您可以使用以下方式进行尝试:
假设您的Follow
表中有一个Follow\u user\u id
$shoutouts = Shoutout::join('follow', 'shoutout.user_id', '=', 'follow.followed_user_id')
->where('follow.user_id', $user->id)
->select(''shoutout.*'')
->orderBy('shoutout.created_at', 'desc')
->get()
您可以使用以下方式进行尝试:
假设您的Follow
表中有一个Follow\u user\u id
$shoutouts = Shoutout::join('follow', 'shoutout.user_id', '=', 'follow.followed_user_id')
->where('follow.user_id', $user->id)
->select(''shoutout.*'')
->orderBy('shoutout.created_at', 'desc')
->get()
只需为数据库::表('users')->join('follow','users.id','=','follow.user_-id')->join('shoutout','follow.follow_-id','=','shoutout.user_-id')->orderBy('shoutout.date','desc->get(),只需为该数据库::表('users')->join('follow','users.id','follow.id','follow.user_-id')->join即可('shoutout'、'follow.followered_user_id'、'='、'shoutout.user_id')->orderBy('shoutout.date'、'desc')->get()这将如何解决按正确顺序获取“shoutouts”的问题?这不是仍然由跟随用户分组,而不考虑最近的“shoutout”吗?你是对的。更新示例时使用了一个关系,该关系将为你提供叫声,只要跟随的用户id\u
更新为你在跟随表中的实际列名,这将如何解决获取“叫声”的问题按照正确的顺序?这不是仍然会被跟踪的用户分组,而不考虑最近的“shoutout”吗你是对的。用一个关系更新了这个例子,只要这个用户id\u被跟踪
被更新为你在跟踪
表中的实际列名,你就会大喊大叫。嗨,阿米特,再次感谢你的帮助!这很有魅力!你和laravel合作多久了?很乐意帮忙:D。很好六个月了,我真的很喜欢。嗨,阿米特,再次感谢你的帮助!这很有魅力!你和拉威尔一起工作多久了?很乐意帮忙:D。六个月了,我真的很喜欢。