laravel5.1(eloquent) order & extend returned collection

12 Views Asked by At

I've got a transaction table using this design:

|id|value|payed_out|...|created_at|updated_at

Now I would like to return all transactions ordered by date and limited to 15 which are:

  • not payed_out (means payed_out == null)
  • not older than 24h (means all transactions which timestamp from now to the timestamp of "created_at" is <24h)

query could look something like:

Transactions::where(payed_out, null)->where(?how to perform the not older check?) ->orderBy('created_at', 'desc') ->take(15)
           ->get();

I would need to "extend" the returned collection of the query I described above.

In my view I would like to display the time until the transaction "expire" means I would need to "add" a field into the collection containing the "time" from the "created_at" field of the transaction until now.

Hope you understand my need.

1

There are 1 best solutions below

0
On

You can try subtracting current timestamp using Carbon. See Carbon Addition and Subtraction. Field created_at must be greater than this timestamp to be considered not older than 1 day. For example:

Transactions::where('payed_out', null)->
          where('created_at', '>', \Carbon\Carbon::now()->subDay())->
          orderBy('created_at', 'desc') ->take(15)->get();