2015-04-07 55 views
0

無論我嘗試什麼,我都會收到同樣的錯誤;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 Model;

<?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的關係特性。現在我只需要讓這部分工作!

+3

它返回數組,而不是讓你應該先用()來呼應說明 –

回答

1

獲取返回結果作爲對象數組,您可以先使用獲取描述。

取代的get()與第()

$descGorLookup = GorLookup::select('description') 
          ->where('oldcode', '=', $value->gor) 
          ->first(); // change here 
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; 
}