How can I change this to get only one post per user:
User::has('posts')->with('posts')->get()
$users = User::has('posts')->with(['posts', function ($query) {
$query->first(); //->take(1)
}])->get()
This one throws an error:
"Illuminate/Database/Eloquent/RelationNotFoundException with message 'Call to undefined relationship [] on model [App/Models/User]."
User hasMany Posts is used.
post
method inside the User class and definehasOne
relationship inside it, then use onlyUser::has('post')->with('post')->get()
. – Ersoy