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"
}

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.

Why the 5G coronavirus conspiracy theory is false – video explainer , So how did the conspiracy incorrectly linking it to 5G start? And is 5G really dangerous? We explain why 5G has nothing to do with Covid-19. Define why. why synonyms, why pronunciation, why translation, English dictionary definition of why. adv. For what purpose, reason, or cause; with what intention, justification, or motive: Why is the door shut?

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

Simon Sinek: How great leaders inspire action, Blue light is scattered more than the other colors because it travels as shorter, smaller waves. This is why we see a blue sky most of the time. Watch this video  why definition: 1. for what reason: 2. used to express surprise or anger: 3. the reasons for something: . Learn more. Cambridge Dictionary +Plus; My profile

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

Why Is the Sky Blue?, Just like that, our internet connection has become an umbilical to the outside world. We now depend on it to do our jobs, to go to school, and to  Why definition, for what? for what reason, cause, or purpose?: Why did you behave so badly? See more.

Why the coronavirus lockdown is making the internet stronger than , Why Subscribe to The New York Times? The Times gives you unparalleled access to the people and events shaping our world today with stories based in facts  Find at least one other person and read Start With Why together. Be it with friends or colleagues, the goal is that we are doing something together. Step 2: Send your book club’s questions for each of the six parts of the book to [email protected] Include the name of your book club and the names of its members.

Why The Times?, Key Words. Amazon's CEO tells investors 'you may want to take a seat,' as he explains why the company will spend 'entirety' of $4 billion profit. Simon Sinek explores how leaders can inspire cooperation, trust and change. He's the author of the classic "Start With Why"; his latest book is "Leaders Eat Last." Portfolio Trade (2011) Buy from Amazon. TEDx was created in the spirit of TED’s mission, “ideas worth spreading.” It supports independent organizers who want to create a TED

Amazon's CEO tells investors 'you may want to take a seat,' as he , Genius spoke to Charlie Harding and Nate Sloan from Vox's Switched On Pop podcast to break down why it's possible for a song to capture the  The resurrection of Jesus Christ is also important because it validates who Jesus claimed to be, namely, the Son of God and Messiah.According to Jesus, His resurrection was the “sign from heaven” that authenticated His ministry (Matthew 16:1–4).

Comments
  • Thanks, like that it appears "strpos() expects parameter 1 to be string, array given".
  • Sorry - typo in the whereHas line. You can try this as is... or maybe just play with a simpler query using the whereDate. Anyway, I edited the typo - it should work now.
  • Thanks, but same issue.
  • Thanks, but same issue. But like that, $pstRegistrations shows an empty collection "LengthAwarePaginator {#271 ▼ #total: 0 #lastPage: 1 #items: Collection {#272 ▼ #items: [] } #perPage: 5 #currentPage: 1 #path: "proj.test/user/profile" #query: [] #fragment: null #pageName: "page" }".
  • But $nextRegistrations is not empty: LengthAwarePaginator {#277 ▼ #total: 1 #lastPage: 1 #items: Collection {#276 ▼ #items: array:1 [▼ 0 => Registration {#280 ▶} ] } #perPage: 5 #currentPage: 1 #path: "proj.test/user/profile" #query: [] #fragment: null #pageName: "page" }.
  • Maybe the issue is in the foreach to show the results.
  • Shouldn't $pstRegistrations be an empty collection? There is only one registration in the future.
  • Yes, it seems correct, but the same issue. But like that it seems that is not possible to get the conference details, like name.