Andrew
Andrew

Reputation: 2198

Laravel 5.1 - compare Carbon dates

Here I need to check does auction for article in my app is live so I write in Article model:

public function scopeCloseauction($query){
        $query->where(Carbon::parse('to')->subDays('auction_end'),'>',Carbon::now());
    }

and view I have:

@if ($article->Closeauction())
                            <td>
                           auction is live
                        </td>
                            @else
                            <td>
                             auction is closed
                            </td>
                        @endif

but I have a problem becouse I got error: enter image description here

UPDATE: I also try: in model to add function:

public function isLive($to,$auction_end) {
    $first = Carbon::create($to).subDays($auction_end);
    $second = Carbon::now();
        return ($first->lte($second));
    }

and in view:

@if ($article->isLive($article->to,$article->auction_end))
                            <td>
                            live
                        </td>
                            @else
                            <td>
                                closed
                            </td>
                        @endif

but now give me this error:

ErrorException in Carbon.php line 425: Unexpected data found. Unexpected data found. Unexpected data found. Trailing data (View: C:\wamp\www\project\resources\views\articles\index.blade.php)

Upvotes: 0

Views: 743

Answers (2)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

You can do something add such function into your Article model:

public function isLive() 
{
    $first = Carbon::parse($this->to)->subDays($this->auction_end);
    $second = Carbon::now();
    return $first->lte($second);
}

and now in your view you can use:

@if ($article->isLive())
    <td>
      live
    </td>
    @else
    <td>
        closed
    </td>
@endif

Upvotes: 1

soywod
soywod

Reputation: 4520

I think your problem is here : Carbon::parse('to'). What do you want to do here ? the Carbon::parse method try to convert a string date into a Carbon date. See the doc. I dont think that Carbon can parse the string 'to'. If you want to check the diff between dates, you should have a look to the appropriated methods like diffInDays.

Upvotes: 0

Related Questions