[英]Why the “@foreach($pastRegistrations as $pastRegistration)” is also returning results?
我有一個UserController,它具有index()方法來獲取auth用戶在會議中的過去和下一次注冊。
然后,在視圖中,我在一個標簽中顯示下一個注冊,在另一個標簽中顯示過去的注冊。
db上只有一個會議,其結束日期為“ 2018-06-15 15:30:00”。 用戶在該會議中只有一個注冊。
因此,日期為“ 2018-06-15 15:30:00”的是下一個注冊,但在過去的選項卡中,注冊用戶中還會出現日期為“ 2018-06-15 15”的會議中的用戶注冊: 30:00" 。 但是它應該僅顯示在下一個注冊選項卡中,因為它是下一個注冊。
您知道為什么foreach“ @foreach($pastRegistrations as $pastRegistration)
”也返回結果嗎?
因此,我在一節中顯示了過去的注冊:
@foreach($pastRegistrations as $pastRegistration)
@if(!empty($pastRegistration->conference || !empty($pastRegistration->conference->start_date)))
<li class="list-group-item">
<h5>{{optional($pastRegistration->conference)->name}}</h5>
</li>
@endif
@endforeach
<div class="text-center d-flex justify-content-center mt-3">
{{$pastRegistrations->fragment('pastConferences')->links("pagination::bootstrap-4")}}
</div>
並顯示下一個注冊:
@foreach($nextRegistrations as $nextRegistration)
@if(!empty($nextRegistration->conference || !empty($nextRegistration->conference->start_date)))
<li class="list-group-item">
<h5>{{optional($nextRegistration->conference)->name}}</h5>
@if ($nextRegistration->status === 'I')
<a href="{{route('conferences.payment',
['id' => $nextRegistration->conference->id,
'regID'=> $nextRegistration->id])}}"
class="btn btn-primary ml-2">Pay
</a>
@endif
</li>
@endif
@endforeach
<div class="text-center d-flex justify-content-center mt-3">
{{$nextRegistrations->fragment('nextConferences')->links("pagination::bootstrap-4")}}
</div>
UserController index()返回過去和下一個注冊:
class UserController extends Controller
{
public function index(Request $request){
$pageLimit = 5;
$user = $request->user();
$pastRegistrations = $user->registrations()->with(['conference' => function ($query) {
$query->where('end_date', '<', now());
}])->paginate($pageLimit);
$nextRegistrations = $user->registrations()->with(['conference' => function ($query) {
$query->where('end_date', '>', now());
}])->paginate($pageLimit);
return view('users.index',
compact('user', 'pastRegistrations','nextRegistrations'));
}
“ dd($ pastRegistrations);” 說明:
LengthAwarePaginator {#276 ▼
#total: 1
#lastPage: 1
#items: Collection {#272 ▼
#items: array:1 [▼
0 => Registration {#270 ▼
#fillable: array:3 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▼
"id" => 1
"status" => "C"
"conference_id" => 1
"main_participant_id" => 1
"created_at" => "2018-06-14 00:09:39"
"updated_at" => "2018-06-14 00:09:39"
]
#original: array:6 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"conference" => null
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
#perPage: 5
#currentPage: 1
#path: "http://proj.test/user/profile"
#query: []
#fragment: null
#pageName: "page"
}
“ dd($ nextRegistrations);” 說明:
LengthAwarePaginator {#279 ▼
#total: 1
#lastPage: 1
#items: Collection {#274 ▼
#items: array:1 [▼
0 => Registration {#280 ▼
#fillable: array:3 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▼
"id" => 1
"status" => "C"
"conference_id" => 1
"main_participant_id" => 1
"created_at" => "2018-06-14 00:09:39"
"updated_at" => "2018-06-14 00:09:39"
]
#original: array:6 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"conference" => Conference {#281 ▼
#fillable: array:18 [▶]
#dates: array:2 [▶]
#appends: array:1 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:23 [▶]
#original: array:23 [▶]
#changes: []
#casts: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
#perPage: 5
#currentPage: 1
#path: "http://proj.test/user/profile"
#query: []
#fragment: null
#pageName: "page"
}
問題可能出在@第2行:
@if(!empty($pastRegistration->conference || !empty($pastRegistration->conference->start_date)))
代替
@if(!empty($pastRegistration->conference) || !empty($pastRegistration->conference->start_date))
看看whereHas方法。 它使您可以基於相關模型的約束來限制結果。
嘗試這個:
$pastRegistrations = $user->registrations()->whereHas('conference', function ($query) {
$query->where('end_date', '<', now());
})->paginate($pageLimit);
$nextRegistrations = $user->registrations()->whereHas('conference', function ($query) {
$query->where('end_date', '>', now());
})->paginate($pageLimit);
約翰(John),比較日期總是有些麻煩,我想查詢無法為您提供正確的信息。 幸運的是,Laravel在Eloquent中有一個內置的解決方案,可以使生活變得更輕松:whereDate() Docs(在頁面下方約2/3)
嘗試將查詢更改為Laravel whereDate函數:
$pastRegistrations = $user->registrations()->whereHas('conference', function ($query) {
$query->whereDate('end_date', '<', now());
})->paginate($pageLimit);
此外,內置的\\ Carbon函數可能值得研究,因為它們還可以使生活變得更加輕松。 如果使用它們,則只需將now()
更改為\\Carbon\\Carbon::now()
在一個簡單的查詢上使用whereDate方法,看看是否確實是麻煩所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.