为什么“@foreach($ pastRegistrations as $ pastRegistration)”也会返回结果?

时间:2018-06-14 14:31:29

标签: php laravel

我有一个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"
}

3 个答案:

答案 0 :(得分:2)

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

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

而不是

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

答案 1 :(得分: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);

答案 2 :(得分:2)

John,比较日期总是有点痛苦,我猜这个查询并没有给你正确的信息。幸运的是,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方法来查看这是否确实存在问题。