如何使用 Laravel 中的帖子查询用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28630850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:58:51  来源:igfitidea点击:

How to query user with posts in Laravel

phplaravel

提问by DNM

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.

我是 Laravel 的新手。我想显示所有帖子,包括发布帖子的用户。我有一个简单的查询,它显示所有帖子,并使用帖子 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?

这工作得很好,但我想与发布帖子的用户一起显示所有帖子。我怎样才能以正确的方式做到这一点?

回答by Tim Lewis

Right now, it is possible to get the Postsby first getting the Userthen using that ID, getting the Posts. Note: this is not the recommended way of using Laravel with relationships:

现在,可以Posts通过先获取 ,User然后使用该 ID,获取Posts. 注意:这不是将 Laravel 用于关系的推荐方式:

$user = User::find($id);
$posts = Post::where("user_id", "=", $user->id)->get();

While this would work (assuming you had a model for Postwith a user_idkey), but the correct way is to define the relationship in the models. For this one, it would be hasMany()and belongsTo()on Userand Postrespectfully. Here is your User.phpmodel class:

虽然这会起作用(假设您有一个Postuser_id键的模型),但正确的方法是定义模型中的关系。对于这一个,这将是hasMany()belongsTo()UserPost尊敬。这是您的User.php模型类:

class User extends Eloquent {
  protected $table = "users";

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

And make sure to define the inverse in your Post.phpmodel class:

并确保在您的Post.php模型类中定义逆:

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();

This will also enforce integrity by forcing any updates/saves to use the right user_idkey. There's some pretty extensive documentation on how to use the Eloquentstyle of database querying, check it out here:

这也将通过强制任何更新/保存使用正确的user_id密钥来强制执行完整性。有一些关于如何使用Eloquent数据库查询样式的非常广泛的文档,请在此处查看:

Laravel - Eloquent

Laravel - 雄辩

Hope that helps!

希望有帮助!

Edit

编辑

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));
}

You will need a file in your app/viewsdirectory named (in this case) view.blade.phpwhich "echos" all the information about the user and the posts:

您将需要在您的app/views目录中创建一个名为(在本例中)的文件,该文件view.blade.php“回显”有关用户和帖子的所有信息:

<h2>User: {{ $user->id }} - {{ $user->email }}</h2>
<br/>
<h2>Posts:</h2>
@foreach($posts AS $post)
<p> {{ $post->id }}: {{ $post->content }}</p>
@endforeach

Etc etc. Note, I have no idea what columns your usersand poststable has, but using $user->columnor $post->columnwill echo the contents of that named column.

等等等等。注意,我不知道你的usersposts表有哪些列,但使用$user->column$post->column将回显该命名列的内容。

Here's the documentation for Views and Responses, which would help you with this issue:

这是 的文档Views and Responses,可以帮助您解决此问题:

Laravel - Views and Responses

Laravel - 视图和响应

回答by Neeraj Tangariya

You can simply use also this.

你也可以简单地使用这个。

public function($id){
$posts = Posts::where('uid','=',auth()->user()->id)->findOrFail($id);
return view('your_blade.php',compact('posts'));

OR either you can return this

return response()->json([$posts], 200);


}

Note: don't forgot to add trait also use App\Posts;

注意:不要忘记添加 trait 也使用 App\Posts;