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, & 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, & 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
@while ($alert)how is this not always true?$alertis empty. You cannot call->alertTitleon empty$alert.dd($alert);in your view to see what it returns. Then you'll know how to handle it