Asked  10 Months ago    Answers:  5   Viewed   5 times

Alright so I have been looking for hours for an answer but can't seem to find one. I have an array of "orders" all for different dates. I get all of the orders for this week, this should not be an issue, then I want to a tab for each day of the week.

What I have tried so far is this:

$dinnerOrders->where('date','>',$date)->where('date','<', $date->endOfDay())->sortBy('created_at');

Where $date is :

$dt = CarbonCarbon::create()->startOfWeek();
CarbonCarbon::setTestNow($dt);
$date = new CarbonCarbon('this ' . $day);

And $dinnerOrders are get by:

$dinnerOrders = collect([]);
foreach($restaurant->dinners as $dinner) {
    foreach($dinner->orders as $order) {
        $dinnerOrders[$order->id] = $order;
    }
 }

The dates seem correct and it words in sql. Is this not working because $dinnerOrders is a collection?

What I'm getting is nothing, i.e an empty set, despite it working in mysql terminal.

So what I'm asking is perhaps, can you do a date comparison on a collection? If so, how?

 Answers

3

Note: This answer is specific to Laravel <= 5.2. Laravel 5.3 updated the where() method on the Collection to match the query builder, so it now accepts an operator.


As you state in your question, $dinnerOrders is a Collection, not a query. The where() method on the Collection works a little differently.

For Collections, the where() method does not accept an operator. It only does equality comparisons. The first parameter is the key, the second is the value, and the third is a boolean to flag a loose comparison (==) vs a strict comparison (===).

What you're looking for is the filter() method. You pass in a Closure that does your date comparison. If the Closure returns true, the item stays in the Collection. If it returns false, the item is removed from the Collection. Something like this (just an example, the logic may need to be tweaked):

$dinnerOrders = $dinnerOrders->filter(function ($item) use ($date) {
    return (data_get($item, 'date') > $date) && (data_get($item, 'date') < $date->endOfDay());
});

Post Question Edit

Based on the code provided in your edit, I'm guessing that a restaurant hasMany dinners, and a dinner hasMany orders. If this is correct, you can setup a relationship stating that a restaurant hasMany orders through dinners:

Restaurant.php:

public function orders() {
    // restaurant has many orders through dinners
    $this->hasManyThrough('AppOrder', 'AppDinner');
}

With this relationship setup, you could then get your information using the query builder:

$dinnerOrders = $restaurant->orders()->where('date','>',$date)->where('date','<', $date->endOfDay())->get();
Friday, August 13, 2021
 
ritch
 
3

Seeing as IlluminateSupportCollection implements ArrayAccess, you should be able to simply use square-bracket notation, ie

$collection[$nth]

This calls offsetGet internally which you can also use

$collection->offsetGet($nth)

and finally, you can use the get method which allows for an optional default value

$collection->get($nth)
// or
$collection->get($nth, 'some default value')
Thursday, April 1, 2021
 
2

You can use one of two ways of creating a Carbon instance from that date string:

1. Create a new instance and pass the string to the constructor:

// From a datetime string
$datetime = new Carbon('2016-01-23 11:53:20');

// From a date string
$date = new Carbon('2016-01-23');

// From a time string
$time = new Carbon('11:53:20');

2. Use the createFromFormat method:

// From a datetime string
$datetime = Carbon::createFromFormat('Y-m-d H:i:s', '2016-01-23 11:53:20');

// From a date string
$date = Carbon::createFromFormat('Y-m-d', '2016-01-23');

// From a time string
$time = Carbon::createFromFormat('H:i:s', '11:53:20');

The Carbon class is just extending the PHP DateTime class, which means that you can use all the same methods including the same constructor parameters or the createFromFormat method.

Thursday, April 1, 2021
 
2

Try a global statement:

$calculation->each(function ($item, $key) {
   global $error_arr;

$error_arr is empty in your code, because it doesn't exist in the scope of the callback function of your each method. Unlike other languages – like JavaScript – variables outside a function aren't accessible by default inside the function. See PHP's documentation on variable scope for details.

Saturday, May 29, 2021
 
3

You need to provide a Carbon date as the first parameter for diffInDays(). So, the logic will be:

CarbonCarbon::parse($shipment->due_date)->diffInDays(now(), false)

Or:

now()->diffInDays(CarbonCarbon::parse($shipment->due_date), false)

Depending on what exactly you're trying to achieve.

false as the second parameter makes the method return signed value (positive or negative).

Friday, August 13, 2021
 
Owen
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :