0
我目前正在用php开发一个新闻源应用程序。用户被分配一个位置。用户可以创建分配了用户位置的帖子。到目前为止,新闻传播正在被所有帖子填充,无论位置如何。我需要的是通过帖子位置与用户位置匹配的帖子填充新闻源。PHP - 如何获取一个表的ID与另一个表相同的帖子?
这是目前获取的职位,按降序排列按创建日期排序它们的代码:
$posts = Post::orderBy('created_at', 'desc')->get();
任何帮助将不胜感激!
我不能得到这个工作,因为我忘记加入我的用户和张贴表。这是我现在的代码,我仍然无法让它工作。
DB::table('posts')
->join('users', function ($join) {
$join->on('users.id', '=', 'posts.user_id')
->where('posts.location', '=', 'users.location');
});
$posts = Post::where('posts.location','users.location')
->orderBy('created_at', 'desc')->get();
return view('dashboard', ['posts' => $posts]);
}
为什么不在'where('location',$ userLocation)'? – nCrazed
详细说明这个问题:据我所知,没有理由使用绕过参数绑定的raw,因为我们不知道位置的来源,可能会引入SQL注入漏洞。 – nCrazed
够公平的,你说得对,没有特别的理由使用原始查询,我开始寻找另一个答案,并以此结束,因为我们没有足够的信息来源于$ users。 –