2

I am building my first db query in laravel4 but I am having some trouble.

My objective is to show site alerts to users once they login. I am getting an error that says: "trying to get property of a non-object"

My model:

class SiteAlert extends Eloquent  {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'sitealerts';


public function scopegetSiteAlerts() {

    $alert = DB::table('sitealerts')
            ->where('isActive', '=', '1')
            ->select('isActive', 'alertTitle', 'alertText', 'alertDate', 'created_at',
                'alertStart', 'alertExpires')
            ->orderBy('created_at', 'desc')
            ->get();
            return $alert;


}


}

My Controller (note: this is my user controller, I don't have a separate controller for my alerts)

public function getdashboard($id)
    {

        //

        $alert = SiteAlert::getSiteAlerts();
        $contractor = Contractor::find($id);
        return View::make('contractors.dashboard')
            ->with('contractor', $contractor)
            ->with('alert', $alert);
    }

And my view file (dashboard.blade.php)

     @if (isset($alert))
<div class="row">
    <div class="span6">
        <img alt="Client logo" src="../assets/images/logo.png" class="avatar" />
        <p class="lead">Howdy, {{$contractor->contact_name; }}</p>
        <p>
            Welcome to your ContractorSherpa Dashboard.<br />
            Your account allows you to view information relating to your projects. You can keep up on progress,
            upload files, make new payments / print receipts, view previous payments, &amp; more.
        </p>
    </div>
    <div class="span6">
        @while ($alert)
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <i class="icon-bullhorn"></i> {{$alert->alertTitle}}
                        <span class="floatRight">
                            {{$alert->alertDate}}
                        </span>
                    </h3>
                </div>
                <div class="panel-body">
                    {{$alert->alertText}}
                </div>
            </div>
        @endwhile
    </div>
</div>
@else
<img alt="Client logo" src="../assets/images/logo.png"  class="avatar" />
<p class="lead">Howdy, {{$contractor->contact_name; }}</p>
<p>
    Welcome to your ContractorSherpa Dashboard.<br />
    Your account allows you to view information relating to your projects. You can keep up on progress,
    upload files, make new payments / print receipts, view previous payments, &amp; more.
</p>
@endif

My guess is that I haven't setup the controller correctly to be able to access the $alert.

Any help would be appreciated. TIA

5
  • 1
    which line triggers an error? Commented Apr 16, 2014 at 20:57
  • @edvinas.me The <i class="icon-bullhorn"></i> {{$alert->alertTitle}} line triggers the error Commented Apr 16, 2014 at 21:18
  • @while ($alert) how is this not always true? Commented Apr 16, 2014 at 21:19
  • it is because $alert is empty. You cannot call ->alertTitle on empty $alert. Commented Apr 16, 2014 at 21:19
  • You can do dd($alert); in your view to see what it returns. Then you'll know how to handle it Commented Apr 16, 2014 at 21:27

3 Answers 3

2

The error is because in your Views you treat variables as objects, when they are passed as an array. Generally, you shuoldn't access Models in your Views, you should have all the logic in Controller (as you do) and then pass data to view with ->with() as an array.

Updated

The problem might be in @while($alert), replace it with this:

                       @foreach ($alert as $a)
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <i class="icon-bullhorn"></i> {{$a->alertTitle}}
                        <span class="floatRight">
                            {{$a->alertDate}}
                        </span>
                    </h3>
                </div>
                <div class="panel-body">
                    {{$a->alertText}}
                </div>
            </div>
        @endforeach
Sign up to request clarification or add additional context in comments.

2 Comments

Actually you are wrong. If you notice, he is using command get(), and that command returns data as object
@Alen I have indeed missed that, the comment regarding the use ofmodels in views still applies though. Answer is updated
0

One of your passed objects seems to be empty. Are your sure that there are data in your database for the given id?

2 Comments

I thought that might be the case too, but the database does have data in it. All rows have a value.
you are passing a colletion of alert-objects into the view. You have to iterate over each element of these collection... use a foreach-construct
0

The problems here are:

// this returns array of stdObjects
$alerts = DB::table('sitealerts')
        ->where('isActive', '=', '1')
        ->select('isActive', 'alertTitle', 'alertText', 'alertDate', 'created_at',
            'alertStart', 'alertExpires')
        ->orderBy('created_at', 'desc')
        ->get();
        return $alerts;

// then in your view you try to get property of array so replace:
@while($alert)
// with:
@foreach ($alerts as $alert) // in your controller use with('alerts',$alerts);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.