Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lijith's avatar

merge two unrelated tables, sort based on date and paginate

i have two tables one for events and other for archives, events have a start and end date and some additional fields and archives have a date and text column

what i want to do is merge them both and sort based on date(for event end date is used) so both the table data is accessible from new-archives page shown in desc order of date

so far i have managed to merge and sort them, how can i paginate it.

$archives = Archive::where('date', '<', Carbon::now()) ->orderBy('date','DESC') ->get();

    $events = Event::where('end_date', '<', Carbon::now())
    ->orderBy('end_date','DESC')
    ->get();

    $dates = array();


    foreach ($archives as $archive) {
        $dt = Carbon::createFromFormat('Y-m-d H:i:s',$archive->date);
        $t = array(
            'date' => $dt->timestamp,
            'id' => $archive->id,
            'type' => 'archive',
            'title' => $archive->title,
            'slug'=> $archive->slug
        );

        array_push($dates,$t);

    }


    foreach ($events as $event) {
        $dt = Carbon::createFromFormat('Y-m-d H:i:s',$event->end_date);
        $t = array(
            'date' => $dt->timestamp,
            'id' => $event->id,
            'type' => 'event',
            'title' => $event->title,
            'slug'=> $event->slug
        );

        array_push($dates,$t);

    }

    array_multisort($dates, SORT_DESC, $dates);
0 likes
0 replies

Please or to participate in this conversation.