skyhunter96
skyhunter96

Reputation: 143

Adding seconds with Carbon

The idea is that I need to get end_date from the Auction via this model, which is inside a database, in MySQL timestamp format (I use phpmyadmin, if it matters, ex. 2018-11-14 04:58:07). So when I get the end_date, the idea is to increment it by few seconds (ex. 10 seconds) via Carbon addSeconds() function and then write it again into the DB. Here is the controller where it's done and my Auction model. What happens is that i get the FatalThrowableError 'Call to a member function addSeconds() on integer'. I played around quite a lot and can't seem to find right format for the secs2 variable.

Auction.php

public $id;
public $name;
public $descript;
public $price;
public $pic;
public $end_date;
public $offers;
public $offered_by;

    public function get(){
    $result =
        DB::table('auctions')
            ->select('*')
            ->where('id', '=', $this->id)
            ->first();
    return $result;
}

    public function increment($id){
    $result =
        DB::table('auctions')
            ->where('id', $id)
            ->update([
                'offers' => DB::raw('offers + 1'),
                'end_date' => $this->end_date
            ]);
    return $result;
}

AuctionController.php

    public function offer($id, Request $request){
    $auction = new Auction();
    $auction->id = $id;

    $secs = $auction->get()->end_date;
    $secs2 = strtotime($secs);
    $secs2->addSeconds(120);

    $auction->end_date = $secs2;
    //dd($secs2);

    $auction->increment($id);
}

Upvotes: 5

Views: 21740

Answers (1)

Jems
Jems

Reputation: 1706

If you want to use addSeconds method, you can't convert your time into number first.. ex:

Carbon::parse($auction->get()->end_date)
      ->addSeconds(120)
      ->format('H:i:s');

Upvotes: 15

Related Questions