Reputation: 19695
When I do:
$tournaments = Tournament::paginate(Config::get('constants.PAGINATION'));
it works, but I just need tournaments that owns the user.
So I do:
$tournaments = Auth::user()->tournaments;
$tournaments->paginate(Config::get('constants.PAGINATION'));
I have a hasMany Relationship in user model
And I get this message
Method paginate does not exist.
How can I solve it? It doesn't seem complicated, but I can't do it!
EDIT: @smartrahat
Result of the second query: Auth::user()->tournaments
Collection {#385 ▼
#items: array:1 [▼
0 => Tournament {#386 ▼
#table: "tournament"
+timestamps: true
#fillable: array:17 [▶]
#dates: array:2 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:16 [▶]
#original: array:16 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
Result of the first query:
LengthAwarePaginator {#428 ▼
#total: 2
#lastPage: 1
#items: Collection {#430 ▼
#items: array:2 [▼
0 => Tournament {#431 ▼
#table: "tournament"
+timestamps: true
#fillable: array:17 [▶]
#dates: array:2 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:16 [▶]
#original: array:16 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => Tournament {#432 ▶}
]
}
#perPage: "20"
#currentPage: 1
#path: "http://laravel.dev:8000/tournaments"
#query: []
#fragment: null
#pageName: "page"
}
Upvotes: 3
Views: 14663
Reputation: 50481
In your question you are calling paginate()
on a Collection more than likely.
In the answer that works you are calling paginate()
on an eloquent builder.
Auth::user()->tournaments; // dynamic property for relationship, probably a collection
Auth::user()->tournaments(); // relation type object (eloquent builder)
Your answer:
$tournaments = Tournament::where('user_id',Auth::user()->id)
->paginate(Config::get('constants.PAGINATION'));
where
is returning the eloquent builder that you are calling paginate
on.
This should give you the same result:
$tournaments = Auth::user()->tournaments()->paginate(...);
Upvotes: 5
Reputation: 19695
I resolved it using:
$tournaments = Tournament::where('user_id',Auth::user()->id)
->paginate(Config::get('constants.PAGINATION'));;
but I still doesn't understand why is it behaving like that
Upvotes: 0