当我这样做时:
$tournaments = Tournament::paginate(Config::get('constants.PAGINATION'));
它有效,但我只需要拥有该用户的锦标赛。
所以我这样做:
$tournaments = Auth::user()->tournaments;
$tournaments->paginate(Config::get('constants.PAGINATION'));
我在用户模型中有一个hasMany Relationship
我收到此消息
Method paginate does not exist.
我该如何解决?它看起来并不复杂,但我无法做到!
编辑: @smartrahat
第二个查询的结果:Auth :: user() - >锦标赛
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
}
]
}
第一次查询的结果:
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"
}
答案 0 :(得分:5)
在您的问题中,您很可能会在集合上调用paginate()
。
在有效的答案中,你在一个雄辩的建设者身上呼叫paginate()
。
Auth::user()->tournaments; // dynamic property for relationship, probably a collection
Auth::user()->tournaments(); // relation type object (eloquent builder)
你的回答:
$tournaments = Tournament::where('user_id',Auth::user()->id)
->paginate(Config::get('constants.PAGINATION'));
where
正在返回您正在调用paginate
的雄辩构建器。
这应该会给你相同的结果:
$tournaments = Auth::user()->tournaments()->paginate(...);
答案 1 :(得分:0)
我使用以下方法解决了这个问题:
$tournaments = Tournament::where('user_id',Auth::user()->id)
->paginate(Config::get('constants.PAGINATION'));;
但我仍然不明白为什么它会像那样