如何在Laravel中用帖子查询用户 [英] How to query user with posts in Laravel
问题描述
我是Laravel的新手.我想显示所有帖子,包括发布帖子的用户.我有一个简单的查询,其中显示所有帖子,并使用帖子ID显示选定的帖子.
I'm new to Laravel. I want to display all post including the user who posted the post. I have a simple query which displays all post, and displays selected post using post id.
public function view_post($id)
{
$post = User::find($id);
dd($post);
}
这工作得很好,但是我想与发布该帖子的用户一起显示所有帖子.我该如何以正确的方式做到这一点?
This is working perfectly fine but I wanted to display all post with the user who posted the post. How can I do this in proper way?
推荐答案
现在,可以通过先获取User
然后使用该ID并获取Posts
来获取Posts
.注意:这不是在关系中使用Laravel的推荐方式:
Right now, it is possible to get the Posts
by first getting the User
then using that ID, getting the Posts
. Note: this is not the recommended way of using Laravel with relationships:
$user = User::find($id);
$posts = Post::where("user_id", "=", $user->id)->get();
这是可行的(假设您有一个使用user_id
键的Post
模型),但是正确的方法是在模型中定义关系.为此,分别是User
和Post
上的hasMany()
和belongsTo()
.这是您的User.php
模型类:
While this would work (assuming you had a model for Post
with a user_id
key), but the correct way is to define the relationship in the models. For this one, it would be hasMany()
and belongsTo()
on User
and Post
respectfully. Here is your User.php
model class:
class User extends Eloquent {
protected $table = "users";
public function posts(){
return $this->hasMany("Post");
}
}
并确保在您的Post.php
模型类中定义逆:
And make sure to define the inverse in your Post.php
model class:
class Post extends Eloquent {
protected $table = "posts";
public function user(){
return $this->belongsTo("User");
}
}
最后,您可以使用以下命令在控制器中查询此关系:
Lastly, you can query this relationship in your controller using the following:
$user = User::find($id);
$posts = $user->posts()->get();
这还将通过强制任何更新/保存使用正确的user_id
键来增强完整性.关于如何使用Eloquent
样式的数据库查询,有一些非常详尽的文档,请在此处查看:
This will also enforce integrity by forcing any updates/saves to use the right user_id
key. There's some pretty extensive documentation on how to use the Eloquent
style of database querying, check it out here:
希望有帮助!
修改
要将其传递给视图,请将以下内容添加到您的控制器中:
To pass this to a view, add the following to your controller:
public function view_post($id){
$user = User::find($id);
$posts = $user->posts()->get();
return View::make("view")->with(array("user" => $user, "posts" => $posts));
}
您将在app/views
目录中需要一个文件(在本例中为view.blade.php
),该文件回避"有关用户和帖子的所有信息:
You will need a file in your app/views
directory named (in this case) view.blade.php
which "echos" all the information about the user and the posts:
<h2>User: {{ $user->id }} - {{ $user->email }}</h2>
<br/>
<h2>Posts:</h2>
@foreach($posts AS $post)
<p> {{ $post->id }}: {{ $post->content }}</p>
@endforeach
等.注意,我不知道您的users
和posts
表具有哪些列,但是使用$user->column
或$post->column
会回显该命名列的内容.
Etc etc. Note, I have no idea what columns your users
and posts
table has, but using $user->column
or $post->column
will echo the contents of that named column.
以下是Views and Responses
的文档,该文档将帮助您解决此问题:
Here's the documentation for Views and Responses
, which would help you with this issue:
这篇关于如何在Laravel中用帖子查询用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!