简体   繁体   中英

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.

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". 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". 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?

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:

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);" 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);" 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:

@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. 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. Luckily Laravel has a built in solution within Eloquent to make life a little easier: whereDate() Docs (about 2/3 down the page)

Try changing your queries to the Laravel whereDate function:

$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. If using them, you would just change now() to \\Carbon\\Carbon::now()

Play around with the whereDate method on a simple query to see if this is indeed where the trouble lies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM