2

I am trying to achieve something like this:
enter image description here

I am using Laravel, and for example to display the results I would do a foreach on the array and display the title, the body etc. Example

@foreach($resArray as $res)
    {{$res->Title}}
@endforeach

And that will display the title of each result in the array, I also have the date in $res->startDate but I am not sure how to list each result with same date under their specific date. You probably didn't understood..

So for example if I have two notifications from 10/07/2017 and one from 11/07/2017 they will display as this:

10/07/2017
- notification 1
- notification 2

11/07/2017
- notification

I was thinking at an if statement but what statement if($res->startDate) what so this won't work either, I was thinking to store them in arrays, for example array of date 11/07/2017 and display those arrays but would that even work...

Couldn't find too much from google as I am not too sure how to google this in a good maneer.

EDIT 1

I tried doing this:

$notifResult = notifications::where('userID', '=', $userID)->select('*')->groupBy(['date'])->get();
        dd($notifResult);

but it didn't work, first of all, I had 3 results in database, it only got 2 of them and it didn't even group them by date, the two results that are listed before are from different days...

enter image description here

EDIT 2

I added toArray to it and this is what I've got: enter image description here

still, it only picks two results...

Community
  • 1
  • 1
Marky
  • 61
  • 1
  • 7
  • 1
    You've said that multiple approaches wouldn't work, but have you actually tried anything? Show an attempt that you've made and describe what problems you're having with it. – Patrick Q Oct 10 '17 at 12:40
  • Possible duplicate of [Converting flat array into an array grouped by categories](https://stackoverflow.com/questions/46490260/converting-flat-array-into-an-array-grouped-by-categories) – GrumpyCrouton Oct 10 '17 at 12:41
  • @PatrickQ But I have no idea how to even write one of those aproaches thats why I was looking for some suggestions. – Marky Oct 10 '17 at 12:42
  • @Marky The possible duplicate that I marked there is very close to the same concept, except change the word "categories" with "dates". The answer there should be able to give you a good idea of what you need to do. – GrumpyCrouton Oct 10 '17 at 12:43
  • @GrumpyCrouton This really helps me, can you post it as an answer so I can mark it as correct? – Marky Oct 10 '17 at 12:44
  • you want to display you data like this - 10/07/2017 - notification 1 - notification 2 11/07/2017 - notification – Suniti Yadav Oct 10 '17 at 12:45
  • @Marky It's already posted as an answer, on the other question. It wouldn't be fair of me to take the credit for it. Just upvote it over there, and this question will likely be closed. – GrumpyCrouton Oct 10 '17 at 12:45
  • laravel has a group by on collection and you can use them – Honarkhah Oct 10 '17 at 13:00
  • group by on db not an answer , because group your data on mysql , you must use group_concat if you want all rows , but on laravel collection work's as you want – Honarkhah Oct 11 '17 at 06:14

2 Answers2

0

One naive but simple solution is to sort the results by start date and then in the foreach check if the previous date (store it in temp var) is different from the current one, and if true display the new date.

$prevDate = '';

@foreach($resArray as $res)
    @if($prevDate != '' && $prevDate != $res->startDate) {
        //do sth to break the html and display the new date
    @endif

    {{$res->Title}}
    {{$prevDate = $res->startDate;}}
@endforeach
  • This is a decent solution. Prevents the overhead of creating a new object or array, and is pretty flexible. – GrumpyCrouton Oct 10 '17 at 12:48
  • I don't think `blade` templating allows assignment like that (unless it's changed recently). `{{ $key = $value; }}` might have to be changed to `` for this to work without throwing an error. – Tim Lewis Oct 10 '17 at 13:59
0

if i understand your problem you must use group by

$collection = collect([
    ['date' => '10/07/2017', 'title' => 'notification 1'],
    ['date' => '10/07/2017', 'title' => 'notification 2'],
    ['date' => '11/07/2017', 'title' => 'notification'],
]);

$grouped = $collection->groupBy('date');

$grouped->toArray();
dd($grouped);

enter image description here

Honarkhah
  • 553
  • 7
  • 19