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');
}
我知道这并没有充分利用 Laravel 的关系功能。现在我只需要让这部分工作!
获取 Return 结果作为对象数组,您可以先使用它来获取描述。
将 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;
}
无论我尝试什么,我总是收到同样的错误;
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 的关系功能。现在我只需要让这部分工作!
获取 Return 结果作为对象数组,您可以先使用它来获取描述。
将 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;
}