php - add row to the DB:: result in Laravel

I have a tableBirthDeath
like below:
id birthmonth deathmonth
1 1 3
2 4 4
3 2 5
The query is for getting either birth or death month,
$events = DB::table('BirthDeath')->where('birthmonth',4)->orWhere('deathmonth',4)->paginate(10);
This query results in 1 row, however, I need this row to be duplicated (because the birth and death month are the same) and in the view, I am intending to show them separately. My current solution is to duplicate the results if the birth and death month are the same but it does not work. Here is the code
events2 = $events;
foreach ($events as $event) {
if ($event->birthmonth == $CurrentMonth || $event->deathmonth == $CurrentMonth) {
$events2->appends($event); <-------- this is the cause of the error
}
}
Answer
Solution:
If you want to add items to a Collection you can use theadd
orpush
method.add
takes a single item andpush
can take many (variable amount of arguments):
$events->add($event);
// or
$events->push($event);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.