Laravel的未定义属性错误


Undefined property error with Laravel

无论我尝试什么,我都会不断收到同样的错误;

Undefined property: Illuminate'Database'Eloquent'Collection::$description

这是我的控制器中的代码;

$gorDistinct = PostcodeExtract::fromTable($tableName)
                ->distinct()
                ->select('gor')
                ->get();
    foreach($gorDistinct as $key => $value)
    {
        print $value->gor;
        $descGorLookup = GorLookup::select('description')
              ->where('oldcode', '=', $value->gor)
              ->get();
        print $descGorLookup->description;
        print "<br>";
        exit;
    }

这是目前我的GorLookup模型;

<?php namespace App'Models;
 use Illuminate'Database'Eloquent'Model;
   class GorLookup extends Model {
   protected $connection = 'postcodes';
   protected $table = 'pc_gor_030315';
   protected $fillable = array('description', 'oldcode');
 }

我知道这并没有充分利用拉拉威尔的关系功能。现在我只需要让这部分工作起来!

获取返回结果作为对象数组,您可以首先使用它来获取描述。

将get()替换为first()

$descGorLookup = GorLookup::select('description')
                           ->where('oldcode', '=', $value->gor)
                           ->first(); // change here
$gorDistinct = PostcodeExtract::fromTable($tableName)
    ->distinct()
    ->select('gor')
    ->first();
foreach($gorDistinct as $key => $value)
{
    print $value->gor;
    $descGorLookup = GorLookup::select('description')
          ->where('oldcode', '=', $value->gor)
          ->first();
    print $descGorLookup->description;
    print "<br>";
    exit;
}