【问题标题】:Why the "@foreach($pastRegistrations as $pastRegistration)" is also returning results?为什么“@foreach($pastRegistrations as $pastRegistration)”也返回结果?
【发布时间】: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"
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    问题可能是第 2 行的拼写错误:

    @if(!empty($pastRegistration->conference || !empty($pastRegistration->conference->start_date)))
    

    而不是

    @if(!empty($pastRegistration->conference) || !empty($pastRegistration->conference->start_date))
    

    【讨论】:

    • 谢谢,但同样的问题。
    【解决方案2】:

    看看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);
    

    【讨论】:

    • 谢谢,但同样的问题。但就像那样,$pstRegistrations 显示一个空集合“LengthAwarePaginator {#271 ▼ #total: 0 #lastPage: 1 #items: Collection {#272 ▼ #items: [] } #perPage: 5 #currentPage: 1 #path:” proj.test/user/profile" #query: [] #fragment: null #pageName: "page" }".
    • 但是 $nextRegistrations 不为空:LengthAwarePaginator {#277 ▼ #total: 1 #lastPage: 1 #items: Collection {#276 ▼ #items: array:1 [▼ 0 => 注册{# 280 ▶} ] } #perPage: 5 #currentPage: 1 #path: "proj.test/user/profile" #query: [] #fragment: null #pageName: "page" }.
    • 也许问题出在 foreach 以显示结果。
    • $pstRegistrations 不应该是一个空集合吗?未来只有一个注册。
    • 是的,这似乎是正确的,但同样的问题。但是这样似乎无法获取会议详细信息,例如名称。
    【解决方案3】:

    约翰,比较日期总是有点麻烦,我猜查询没有给你正确的信息。幸运的是,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 方法,看看这是否确实是问题所在。

    【讨论】:

    • 谢谢,好像“strpos() 期望参数 1 是字符串,给定数组”。
    • 对不起 - whereHas 行中的错字。您可以按原样尝试这个......或者也许只是使用 whereDate 来玩一个更简单的查询。无论如何,我编辑了错字 - 它现在应该可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多