Laravel数据库查询

时间:2014-04-16 20:55:57

标签: php laravel laravel-4

我正在laravel4中构建我的第一个数据库查询但是我遇到了一些麻烦。

我的目标是在用户登录后向用户显示网站提醒。我收到的错误是:“试图获取非对象的属性”

我的模特:

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;


}


}

我的控制器(注意:这是我的用户控制器,我的警报没有单独的控制器)

public function getdashboard($id)
    {

        //

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

我的视图文件(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

我的猜测是我没有正确设置控制器以便能够访问$ alert。

任何帮助将不胜感激。 TIA

3 个答案:

答案 0 :(得分:2)

错误是因为在视图中,当变量作为数组传递时,您将变量视为对象。通常,您不需要访问视图中的模型,您应该拥有Controller中的所有逻辑(就像您一样),然后将数据传递给->with()作为数组进行查看。

<强>更新

问题可能出在@while($alert),请将其替换为:

                       @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

答案 1 :(得分:0)

您传递的某个对象似乎是空的。您确定数据库中是否存在给定ID的数据?

答案 2 :(得分:0)

这里的问题是:

// 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);