0

Currently, my application uses quite a few foreach loops, to iterate through an array pulled through the database.

In order for the user to access the page, whether they have 0,1 or 100 results, I have to use three IF statements, to stop the foreach loop from breaking. This check is solely for 0 and 1 results, anything greater works fine.

I imagine I'll have several more pages to iterate through certain database results, and surely using 3 IF statements isn't the most efficient way?

An example:

Controller:

public function getArchived()
    {
        $alert = Alert::with('location')->where('user_id', '=', Auth::user()->id)
                                        ->onlyTrashed()
                                        ->first();
        $this->layout->content = View::make('agents.alert.archived', 
                                             array('alert' => $alert));
    }

View (shortened):

@if((count($alert) > 1)
    @foreach($alert->alerts as $alert)
        {{ $alert->id }}
    @endforeach
@elseif ((count($alert) > 0)
        {{ $alert->id }}    
@else
    <p>You have no results</p>
@endif

Is there a check one can do on the controller prior to sending the array to the view? Any guidance would be appreciated

2
  • You use the first()-Function. You always get only one result, didnt you? Please show more code! Commented Apr 23, 2014 at 18:41
  • What further code do you need to see @Fuzzyma? Commented Apr 23, 2014 at 18:43

1 Answer 1

2

Currently you don't need to do those checks. You are using first() to get a single object.

I think what you want to do is use get() instead to get a collection of archived Alerts.

$alerts = Alert::with('location')->where('user_id', '=', Auth::user()->id)
                                    ->onlyTrashed()
                                    ->get();
$this->layout->content = View::make('agents.alert.archived', 
                                         array('alerts' => $alerts));

Now you know you will be receiving a Collection that can be iterated.

    @foreach($alerts as $alert)
        {{ $alert->id }}
    @endforeach
    @if($alerts->empty())
        No Records
    @endif

Update:

In Laravel 4.2, Blade now supports this type of syntax:

@forelse($whole as $piece)
    {{ $piece->something }}
@empty
    No Records
@endforelse
Sign up to request clarification or add additional context in comments.

7 Comments

Hi @Drew Lewis, thank you for your reply. This makes perfect sense. The error now appears to be when accessing a relationship within the foreach loop, for example: {{ $alert->location->address_1}}. I know this is correct as it did work on the previous ->first() incorrect statement. An alert belongs to location.
ErrorException - Trying to get property of non-object, and it's on this line inside the foreach - {{ $alert->location->address_1}}.
@Svengali make sure every one of those alerts has a location
They all definitely do. Like I said, when I was using ->first(), this successfully retrieved the locations for each of the alerts, I'm not sure why it would change now?
It's also the same now with trying to access messages relating to the alert. Each message belongs to an alert (by foreign key). $alert->messages->count() now also fails.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.