2

I have a little problem but it's frustrating me a lot.

I have two tables
1. users
2. posts

The user can have many posts and the post belongs to one user

My question : how to get the first post when typing the relation?

public function index() {
 $user = User::find(1); 

// here it will return all posts

$user->with('posts');

    return view('home.index',compact('user'));
}

By the way : i used closure but it didn't work ... it return all users with posts

public function index()
    {

        $user = User::find(1);


        $user = $user->with(['posts' => function ($query) {
            $query->first();
        }])->get();

        return $user ;
        return view('home.index',compact('user'));
    }

and the result is like that :

[
{
"id": 1,
"name": "Barney Walter",
"email": "wuckert.murphy@example.org",
"created_at": "2016-12-22 10:48:45",
"updated_at": "2016-12-22 10:48:45",
"posts": []
},
{
"id": 2,
"name": "Dr. Makenzie Rutherford",
"email": "estroman@example.com",
"created_at": "2016-12-22 10:48:45",
"updated_at": "2016-12-22 10:48:45",
"posts": []
},
{
"id": 3,
"name": "Ernesto Shanahan",
"email": "pwill@example.com",
"created_at": "2016-12-22 10:48:45",
"updated_at": "2016-12-22 10:48:45",
"posts": []
},
{
"id": 4,
"name": "Rosalinda Cartwright",
"email": "josefa.murazik@example.com",
"created_at": "2016-12-22 10:48:45",
"updated_at": "2016-12-22 10:48:45",
"posts": []
},
{
"id": 5,
"name": "Kyleigh Willms",
"email": "ddach@example.net",
"created_at": "2016-12-22 10:48:45",
"updated_at": "2016-12-22 10:48:45",
"posts": [
{
"id": 1,
"user_id": 5,
"name": "Malcolm Simonis",
"title": "Autem quidem quia earum ipsam. A a commodi id adipisci quia minima iste numquam. Eum eius odit porro veniam. Et iure occaecati sapiente minima et beatae. Labore eius labore accusamus sapiente sed eveniet accusamus.",
"is_published": 1,
"created_at": "2016-12-22 10:54:14",
"updated_at": "2016-12-22 10:54:14"
}
]
}
]

any help about that ... please !!

Imanol
  • 4,824
  • 2
  • 28
  • 35
hikefd
  • 291
  • 1
  • 4
  • 13
  • I'm not entirely sure what answer to give because your question is unclear. What should the intended result be? Do you want to filter the users based on having one or more posts or do you want to show all users and only their first post? – Luceos Dec 22 '16 at 12:11
  • Not know Laravel as Much, But based on Logic, i can say that you can try Pagination or Paged Query for your Posts result. and than you can get how many Posts you need. Something like [this](http://stackoverflow.com/questions/21255508/select-the-first-10-rows-laravel-eloquent?answertab=active#tab-top) – Hardik Bharadava Dec 22 '16 at 12:13
  • yes ... I want to show specific user and only his first post ? – hikefd Dec 22 '16 at 12:14

3 Answers3

0

Try to use take():

$user = User::with(['posts' => function ($query) {
            $query->orderBy('created_at', 'asc')->take(1);
        }])->first();

And orderBy('created_at', 'asc') will sort result, so you'll take the very first post.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • This is almost right. You just have to replace the `first()` method with the `find($id)` method for the User model. The `with()` closure looks right to me. – Carter Fort Dec 22 '16 at 14:12
0

Try this you will get just 1 post.

$user = User::with('posts')->first();
$user->setRelation('posts', $business->posts->take(1));
Amit Gupta
  • 17,072
  • 4
  • 41
  • 53
Pooja JaJal
  • 293
  • 1
  • 4
0

If you want a model object of post instead of collection then you can define a new hasOne relation in User model as:

public function post()
{
    return $this->hasOne('App\Post');
}

And then you can query it as:

$user = User::where('id', 1)->with('post')->first(); 
Amit Gupta
  • 17,072
  • 4
  • 41
  • 53