【发布时间】:2018-11-24 09:00:20
【问题描述】:
我有一个 UserController,它有一个 index() 方法来获取 auth 用户在会议中的过去和下一个注册。
然后在视图中,我在一个标签中显示下一个注册,在另一个标签中显示过去的注册。
db 上只有一个会议,end_date 为“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"
}
【问题讨论】: