Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如何获取跟踪用户的帖子_Laravel_Eloquent_Laravel 5.4_Feed - Fatal编程技术网

Laravel 如何获取跟踪用户的帖子

Laravel 如何获取跟踪用户的帖子,laravel,eloquent,laravel-5.4,feed,Laravel,Eloquent,Laravel 5.4,Feed,我正在尝试创建新闻提要页面,但在此页面中,我需要输出按日期排序的文章,而且它们必须属于授权用户所遵循的用户。我尝试了一些事情,然后意识到我的逻辑是错误的。因此在用户模型中我有: function followers() { return $this->belongsToMany('App\User', 'followers', 'user_id', 'follower_id'); } function follows() { return $this->belong

我正在尝试创建新闻提要页面,但在此页面中,我需要输出按日期排序的
文章,而且它们必须属于授权用户所遵循的用户。我尝试了一些事情,然后意识到我的逻辑是错误的。因此在
用户模型中
我有:

function followers()
{
    return $this->belongsToMany('App\User', 'followers', 'user_id', 'follower_id');
}

function follows()
{
    return $this->belongsToMany('App\User', 'followers', 'follower_id', 'user_id');
}

function articles(){
    return $this->hasMany('App\Article');
}
而在
文章中
我有以下关系:

public function user()
{
    return $this->belongsTo('App\User');
}
我不知道是否可以简单地获取所有文章,然后输出用户关注的文章。我有点困惑

以下是我在控制器中使用的代码:

 $followinguserarticle = Article::whereHas('user.follows', function($q) {
            $q->where('id', auth()->id());
        })
            ->latest()
            ->get();
然后我试着用以下方法循环:

@foreach($followinguserarticle as $followinguserarticles)
    <a>followinguserarticles->title</a>
@endforeach
@foreach($followUserArticles作为$followUserArticles)
以下用户文章->标题
@endforeach
使用:

使用:

您可以在已授权的用户实例上快速加载关系

$authed = Auth::user();

$authed ->load('follows.articles');
现在您有了用户关注的用户文章

foreach($authed->follows as $follow){
   foreach($follow->articles as $article){

       //Now you have access to each article
   }
}
您可以在已授权的用户实例上快速加载关系

$authed = Auth::user();

$authed ->load('follows.articles');
现在您有了用户关注的用户文章

foreach($authed->follows as $follow){
   foreach($follow->articles as $article){

       //Now you have access to each article
   }
}

所以我找到的解决方案是这样的:

//get list of follower ids
$follows = Auth::user()->follows->pluck('id');

//get articles of those that user follows
$articles = Article::whereIn('user_id',$follows)
                      ->with('user')
                      ->latest()
                      ->limit(10)
                      ->get();

所以我找到的解决方案是这样的:

//get list of follower ids
$follows = Auth::user()->follows->pluck('id');

//get articles of those that user follows
$articles = Article::whereIn('user_id',$follows)
                      ->with('user')
                      ->latest()
                      ->limit(10)
                      ->get();


试着从数据库中获取数据,看看它是否按预期工作。我想我现在有一个更大的问题,我甚至无法返回并循环阅读文章。它说为foreach()提供的参数无效@Sohel0415您知道这可能是什么原因吗?用return语句显示您的完整控制器代码尝试从数据库中获取数据,看看它是否按预期工作。我想我现在有一个更大的问题,我甚至无法返回和循环文章。它说为foreach()提供的参数无效@Sohel0415您知道这可能是什么原因吗?显示完整的控制器代码和返回语句当您执行
User::find(Auth::id())
时,您正在执行另一个查询以加载已由Laravel加载的已验证用户实例。此外,文章将不会按OP希望的日期排序,它们将按用户分组且未排序。仍然未排序。此外,OP希望将结果限制为10。当您执行
User::find(Auth::id())
时,您将执行另一个查询以加载已由Laravel加载的已验证用户实例。此外,文章将不会按OP希望的日期排序,它们将按用户分组且未排序。仍然未排序。另外,OP希望将结果限制在10以内。@Nikonah是的,我对这些关系感到有点困惑。我首先发布了
之后的
,然后将其更改为
追随者
。请两者都试试。我假设我应该给出一个变量并将查询分配给它,然后我需要在视图中用foreach循环它,对吗?如果是这样的话。它抛出错误:为foreach()@Nikonah提供的参数无效。即使未找到任何项目,它也不应给出错误。请显示您正在使用的代码。@Nikonah代码是正确的。确保将
$followUserArticle
从控制器传递到显示的视图。如果您这样做,请显示
{{dd($followUserArticle)}}
的结果为了澄清我的问题,我想显示跟踪用户的文章。就这些,我只想按时间顺序排列,并输出10个them@Nikonah是的,我对这些关系有点困惑。我首先发布了
之后的
,然后将其更改为
追随者
。请两者都试试。我假设我应该给出一个变量并将查询分配给它,然后我需要在视图中用foreach循环它,对吗?如果是这样的话。它抛出错误:为foreach()@Nikonah提供的参数无效。即使未找到任何项目,它也不应给出错误。请显示您正在使用的代码。@Nikonah代码是正确的。确保将
$followUserArticle
从控制器传递到显示的视图。如果您这样做,请显示
{{dd($followUserArticle)}}
的结果为了澄清我的问题,我想显示跟踪用户的文章。就这些,我只想按时间顺序排列,并输出10个