Laravel的未定义属性错误

时间:2015-04-07 12:49:43

标签: php laravel eloquent

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

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');
 }

据我所知,这并没有充分利用Laravel的关系功能。现在我只需要让这部分工作!

2 个答案:

答案 0 :(得分:1)

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

用第一个()

替换get()
$descGorLookup = GorLookup::select('description')
                           ->where('oldcode', '=', $value->gor)
                           ->first(); // change here

答案 1 :(得分:1)

$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;
}