2

I am trying to filter a collection of objects by comparing date its expiry date with today's date but I cant quite get it to work. Can anyone take a look at the code. I'm sure i'm missing something here. I have searched all over and found alot of examples but none are working.

This complies just fine but with a list of 9 objects 3 of which have an expiry date which is set to 2012 it doesn't return any results from the collection.

Controller

public function classifieds()
{   
    $myclassifieds = Auth::user()->classifieds;
    return view('account.classifieds')->with(['allclassifieds'=>$myclassifieds]);
}

View

@if(count($allclassifieds->where('expired_on','<', Carbon\Carbon::now() ) ) > 0)
//Something here
@endif
matpulis
  • 123
  • 3
  • 11
  • Looks fine, hard to say without seeing the collection itself. Are you sure that `expired_on` is available directly on the first level of each collection object? Also, `where()` comparison is strict. Could be that your `expired_on` is a date, while Carbon::now() gives you datetime - and that might cause problems, not sure. – lesssugar Apr 05 '16 at 19:05
  • This is the collection with one object in it. Expires_on is a date time object its very strange. [ { "id":31, "username":"test", "type":1, "action":"sell", "maincat":"Antiques", "title":"test ", "description":"test", "extratextfield":null, "lowerprice":1, "partex":0, "condition":"used", "price":"680", "images":"1", "email":"test@test.com", "comments":1, "created_at":"2016-04-03 19:00:26", "updated_at":"2016-04-03 19:00:26", "expires_on":"2016-03-14 19:00:26" } ] – matpulis Apr 05 '16 at 19:11
  • In your code, you have `expired_on`. Is it a typo while putting this in the post or a typo in the original code? ;) The object holds `expires_on` in it. – lesssugar Apr 05 '16 at 19:19
  • thats my bad its actually written good in the view – matpulis Apr 05 '16 at 19:28

1 Answers1

1

Access the value expired_on directly

@foreach($allclassifieds as $classified)
    @if($classified->expired_on > Carbon\Carbon::now())
        // show classified
    @endif
@endforeach

You can make the expired_on property a Carbon instance by default by adding it to the $dates array in your model:

protected $dates = ['created_at', 'updated_at', 'expired_on'];

now expired_on always returns a Carbon instance

Angad Dubey
  • 5,067
  • 7
  • 30
  • 51
  • This does actually work but I have to Carbon::parse() the $classified->expired_on to sucessfully compare. Not exactly what im looking for but its doable this way – matpulis Apr 05 '16 at 19:13
  • @matpulis updated answer. Could you elaborate what you are looking for. – Angad Dubey Apr 05 '16 at 19:33
  • I want to get a count of how many items in the collections have an expires_on date smaller then today. without actually having to do a for loop. Altough your method does work. Thanks you – matpulis Apr 05 '16 at 19:35
  • @matpulis oh I see. I would have the controller handle that. and send it as a variable to the view. – Angad Dubey Apr 05 '16 at 19:36
  • I did just that. Strangely enough ->where('expired_on','<', Carbon\Carbon::now()) works good when you put it directly to the model as a query. Thanks man – matpulis Apr 05 '16 at 19:57