gpt4 book ai didi

php - 为什么 "@foreach($pastRegistrations as $pastRegistration)"也返回结果?

转载 作者:行者123 更新时间:2023-12-03 18:41:31 24 4
gpt4 key购买 nike

我有一个 UserController,它有一个 index() 方法来获取 auth 用户在 session 中的过去和下一次注册。

然后在 View 中,我在一个选项卡中显示下一次注册,在另一个选项卡中显示过去的注册。

db 上只有一个 session ,结束日期为“2018-06-15 15:30:00”。并且用户在该 session 中只有一个注册。

因此日期为“2018-06-15 15:30:00”的是下一次注册,但在过去注册的选项卡中也出现了 session 中用户的注册日期为“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"
}

最佳答案

约翰,比较日期总是有点麻烦,我猜查询没有给你正确的信息。幸运的是,Laravel 在 Eloquent 中有一个内置的解决方案,让生活变得更轻松:whereDate() Docs (about 2/3 down the page)

尝试将查询更改为 Laravel whereDate 函数:

$pastRegistrations = $user->registrations()->whereHas('conference', function ($query) {
$query->whereDate('end_date', '<', now());
})->paginate($pageLimit);

此外,内置的\Carbon 函数可能值得研究,因为它们也可以让生活变得更轻松。如果使用它们,您只需将 now() 更改为 \Carbon\Carbon::now()

在一个简单的查询中尝试使用 whereDate 方法,看看这是否确实是问题所在。

关于php - 为什么 "@foreach($pastRegistrations as $pastRegistration)"也返回结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50859924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com