[英]Why the “@foreach($pastRegistrations as $pastRegistration)” is also returning results?
I have a UserController that has an index() method to get the past and next registrations of the auth user in conferences. 我有一个UserController,它具有index()方法来获取auth用户在会议中的过去和下一次注册。
Then in the view, I show in a tab the next registrations and in another tab the past registrations. 然后,在视图中,我在一个标签中显示下一个注册,在另一个标签中显示过去的注册。
There is only one conference on db and has the end_date "2018-06-15 15:30:00". db上只有一个会议,其结束日期为“ 2018-06-15 15:30:00”。 And the user has only one registration in that conference.
用户在该会议中只有一个注册。
So with the date "2018-06-15 15:30:00", is a next registration, but in the tab of the past registrations is also appearing the registration of the user in conference with date "2018-06-15 15:30:00". 因此,日期为“ 2018-06-15 15:30:00”的是下一个注册,但在过去的选项卡中,注册用户中还会出现日期为“ 2018-06-15 15”的会议中的用户注册: 30:00" 。 But it should only appear in the next registrations tab since is a next registration.
但是它应该仅显示在下一个注册选项卡中,因为它是下一个注册。
Do you know why the foreach " @foreach($pastRegistrations as $pastRegistration)
" is also returning a result? 您知道为什么foreach“
@foreach($pastRegistrations as $pastRegistration)
”也返回结果吗?
So I have in a section to show the past registrations: 因此,我在一节中显示了过去的注册:
@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>
And to show the next registrations: 并显示下一个注册:
@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() that returns past and next registrations: 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'));
}
The "dd($pastRegistrations);" “ dd($ pastRegistrations);” shows:
说明:
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"
}
The "dd($nextRegistrations);" “ dd($ nextRegistrations);” shows:
说明:
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"
}
Could the problem be the typo @ row 2: 问题可能出在@第2行:
@if(!empty($pastRegistration->conference || !empty($pastRegistration->conference->start_date)))
instead of 代替
@if(!empty($pastRegistration->conference) || !empty($pastRegistration->conference->start_date))
Take a look at the whereHas method. 看看whereHas方法。 It lets you limit the results based on constraints on related models.
它使您可以基于相关模型的约束来限制结果。
Try this: 尝试这个:
$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, comparing dates is always a bit of a pain, and I'm guessing the query is not giving you the right information. 约翰(John),比较日期总是有些麻烦,我想查询无法为您提供正确的信息。 Luckily Laravel has a built in solution within Eloquent to make life a little easier: whereDate() Docs (about 2/3 down the page)
幸运的是,Laravel在Eloquent中有一个内置的解决方案,可以使生活变得更轻松:whereDate() Docs(在页面下方约2/3)
Try changing your queries to the Laravel whereDate function: 尝试将查询更改为Laravel whereDate函数:
$pastRegistrations = $user->registrations()->whereHas('conference', function ($query) {
$query->whereDate('end_date', '<', now());
})->paginate($pageLimit);
Also, the built-in \\Carbon functions may be worth investigating, as they can also make life a lot easier. 此外,内置的\\ Carbon函数可能值得研究,因为它们还可以使生活变得更加轻松。 If using them, you would just change
now()
to \\Carbon\\Carbon::now()
如果使用它们,则只需将
now()
更改为\\Carbon\\Carbon::now()
Play around with the whereDate method on a simple query to see if this is indeed where the trouble lies. 在一个简单的查询上使用whereDate方法,看看是否确实是麻烦所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.