Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could a way to loop through a query for a regular schedule i.e. $openingHours->forWeek(); be included within the Usage section please? #51

Closed
JeftaChibiya opened this issue Dec 19, 2017 · 4 comments

Comments

@JeftaChibiya
Copy link

Attemping to use a foreach loop to display the results of the query: $openingHours->forWeek(); returns an empty result. On the other hand, dd($openingHours->forWeek()) returns the array as expected.

@JeftaChibiya JeftaChibiya changed the title Could a way to loop through a query for a regular schedule i.e. $openingHours->forWeek(); within the Usage section please? Could a way to loop through a query for a regular schedule i.e. $openingHours->forWeek(); be included within the Usage section please? Dec 19, 2017
@sebastiandedeyne
Copy link
Member

That's really odd, since the method simply returns an array. Could you share a full code snippet that doesn't work as expected, or PR a failing test?

@JeftaChibiya
Copy link
Author

JeftaChibiya commented Dec 21, 2017

Sure! Here's what I have:

The method in controller:

  public function about()
    {
        $openingHours = OpeningHours::create([
            'monday' => ['09:00-12:00', '13:00-18:00'],
            'tuesday' => ['09:00-12:00', '13:00-18:00'],
            'wednesday' => ['09:00-12:00'],
            'thursday' => ['09:00-12:00', '13:00-18:00'],
            'friday' => ['09:00-12:00', '13:00-20:00'],
            'saturday' => ['09:00-12:00', '13:00-16:00'],
            'sunday' => [],
            'exceptions' => [
                '08-26' => ['10:00-20:00'],
                '01-01' => [], // Recurring on each 1st of january
                '12-25' => ['09:00-12:00'], // Recurring on each 25th of december
            ],  
        ]);

        $thisWeek = $openingHours->forWeek(); 
        
        return view('about', compact('thisWeek'));        
    }

And in the view:

      @foreach($thisWeek as $day)
           {{ $day }}
         //  I'm actually not too sure what attributes I can extract from each single item here
      @endforeach

Result:
ErrorException: htmlspecialchars() expects parameter 1 to be string, object given

@JeftaChibiya
Copy link
Author

I really appreciate your time and help @sebastiandedeyne.
Could you please share with me an example of how you would loop through it?

Thanks!

@sebastiandedeyne
Copy link
Member

@foreach($thisWeek as $day => $openingHours)
           {{ $day }}
@endforeach

$day will contain the day name. openingHours will be an object containing the hours for that day. Refer to the OpeningHoursForDay class to see what you can extract from it :)

https://github.com/spatie/opening-hours/blob/master/src/OpeningHoursForDay.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants