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

saurav77's avatar

How to merge two model and get their date by grouping ?

Suppose I have two tables with the same name column. (action_date).I want to merge two models and get action dates by grouping them in the array.

public function getDates(){
	$bills=Bill::groupby(['action_date'])
             ->orderBy('action_date')
             ->get();
  $orders=Order::groupby(['action_date'])
             ->orderBy('action_date')
             ->get();
$query = collect($orders)->merge(collect($bills));
$dates=$query->toArray();
	 array_walk_recursive($dates, function(&$item){
            if(is_object($item)) $item = (array)$item;
        });
        $action_date = array_unique (array_column($dates, 'action_date'));
}

I am getting result like this

array:4 [▼
  0 => "2021-03-25 00:03:00"
  1 => "2021-03-27 00:00:00"
  2 => "2021-03-27 00:03:00"
  3 => "2021-03-31 00:03:00"
]

But I am supposed to get this type of result

array:4 [▼
  0 => "2021-03-25 00:03:00"
  1 => "2021-03-27 00:00:00"
  2 => "2021-03-31 00:03:00"
]
0 likes
3 replies
chaudigv's avatar

But I am supposed to get this type of result

Just so it's clear to me, you want unique dates (irrespective of time) after merging.

saurav77's avatar

Sorry, My mistake.

I get two result even there are same date because of time I think

Please or to participate in this conversation.