欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to query user with posts articles in Laravel 得到用户的 articles 的数组

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?

shareimprove this question
 
1 
Do you have any idea about laravel Eloquent model? – Safoor Safdar Feb 20 '15 at 14:31
   
Did you defined the Models relationship? laravel.com/docs/4.2/eloquent#relationships – Th3Alchemist Feb 20 '15 at 14:33
1 
This is CLEARLY EXPLAINED in the official Laravel documentation: laravel.com/docs/master/eloquent#eager-loading It would do you some good to read it before asking simple questions. – Martin Bean Feb 20 '15 at 14:54
   
Read the book from Dayle Reese! It's very good for Laravel beginners. daylerees.com/codebright – DaenuFeb 20 '15 at 15:24

1 Answer 正确答案 

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

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

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

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:

Laravel - Eloquent

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/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

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.

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

Laravel - Views and Responses

shareimprove this answer
 
   
$posts = $user->posts()->get() this line will get all the post of a user right? how about if i only want to display a post with the user who posted it? – sean arce Feb 20 '15 at 14:44
   
You pass both the $user and $posts to a view and process them there. I'll make a quick edit. – Tim Lewis Feb 20 '15 at 14:45
   
Now, if you want to show all posts with their user, do Post::get(), loop though each $posts AS $postand call $post->user()->first to get their info, such as name and email. – Tim Lewis Feb 20 '15 at 14:54
   
its ok i understand. its just that the method is kinda blurry. but now its much clearer thanks. i will try your code. – sean arce Feb 20 '15 at 14:56
1 
@TimLewis You’re pointing @seanacre in totally the wrong direction. He wants to get the user of a post: so $post = $post->with('user')->find(1); $user = $post->user; will do it. – Martin Bean Feb 20 '15 at 15:03


来自  https://stackoverflow.com/questions/28630850/how-to-query-user-with-posts-in-laravel

 


普通分类: