Php Laravel 5无法从模型中访问心房
我遇到了一个小问题,由于某种原因,我无法从关系模型访问我的属性 我一直收到相同的错误,“未定义属性:illumb\Database\elounce\Collection::$seasure” 我整天都在盯着这件事,我找不到解决办法Php Laravel 5无法从模型中访问心房,php,laravel,model,eloquent,Php,Laravel,Model,Eloquent,我遇到了一个小问题,由于某种原因,我无法从关系模型访问我的属性 我一直收到相同的错误,“未定义属性:illumb\Database\elounce\Collection::$seasure” 我整天都在盯着这件事,我找不到解决办法 <?php namespace App\Http\Controllers; use App\Show; use App\Episode; class TestController extends Controller { public functi
<?php namespace App\Http\Controllers;
use App\Show;
use App\Episode;
class TestController extends Controller {
public function test(){
$shows = Show::where('active',1)->get();
foreach ($shows as $show) {
if($show->active == 1 && $show->airday == date('N'))
{
echo 'test';
$episode = $show->episodes()->orderBy('id', 'desc')->take(1)->get();
\Debugbar::info($episode);
//this is the line where he gives me
echo('New episodes avaible, Season '.$episode->season.' Episode '.$episode->episode.' From '.$show->name);
//end error
}
}
}
}
?>
目前我不知道代码有什么问题。
我希望有人能给我指出正确的方向。当你打电话给->take(1)->get()
时,你是在告诉雄辩的(真正的建设者)给你一个集合
对象,里面有一个项目
因此,当您试图访问$seasure
属性时,实际上是在集合对象上访问它,而不是您认为的模型
你可以做几件事:
->take(1)->get()
调用替换为->first()
$spice
变量视为一个集合,并通过访问$spice[0]
来检索您关心的实际插曲$spoint
变量($spoint=$spoint->first()
)调用first
,以获取其中的第一个模型对象嘿,吉姆,谢谢你的回答,这很有道理,谢谢!
Illuminate\Database\Eloquent\Collection {#150
#items: array:1 [
0 => App\Episode {#241
#table: "episodes"
+timestamps: true
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:6 [
"id" => "6"
"show_id" => "6"
"season" => "3"
"episode" => "15"
"created_at" => "2016-02-07 11:45:53"
"updated_at" => "2016-02-07 11:45:53"
]
#original: array:6 [
"id" => "6"
"show_id" => "6"
"season" => "3"
"episode" => "15"
"created_at" => "2016-02-07 11:45:53"
"updated_at" => "2016-02-07 11:45:53"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]