Php 未定义的属性:Illumb\Database\Eloquent\Collection::$id Laravel 4

Php 未定义的属性:Illumb\Database\Eloquent\Collection::$id Laravel 4,php,collections,properties,undefined,Php,Collections,Properties,Undefined,我用的是LaravelV4.2。。 我想创建更新记录。 你能帮助我吗。。这个代码有什么问题。。。 这是我的代码: MatakuliahsController.php 公共功能编辑($id) { //$matakuliahs=$this->matakuliahs->find($id); $matakuliahs=Matakuliah::where('id','=',$id)->get(); 如果(为空($matakuliahs)){ return Redirect::route('matakuli

我用的是LaravelV4.2。。 我想创建更新记录。 你能帮助我吗。。这个代码有什么问题。。。 这是我的代码:

MatakuliahsController.php

公共功能编辑($id) { //$matakuliahs=$this->matakuliahs->find($id); $matakuliahs=Matakuliah::where('id','=',$id)->get(); 如果(为空($matakuliahs)){ return Redirect::route('matakuliahs.index'); } 返回视图::make('matakuliahs.edit',compact('matakuliahs'); } edit.blade.php

{Form::open(数组('autocomplete'=>'off','method'=>'PATCH','route'=>array('matakuliahs.update',$matakuliahs->id))} ... {{Form::close()}} 错误是:

未定义的属性:Illumb\Database\Elounce\Collection::$id(视图:C:\xampp\htdocs\Laravel 4\projectLaravel\app\views\matakuliahs\edit.blade.php)
感谢您的关注和帮助。

在控制器中尝试以下方法:

$matakuliahs = Matakuliah::find($id);

然后将其传递给视图。

您试图获得的是模型集合上的关系,该关系存在于该集合中的对象上。可以使用first()返回第一个,也可以使用loop for each-one-get-the-items

    $matakuliahs = Matakuliah::where('id','=',$id)->get()->first();
返回id等于$id的对象集合。在这种情况下,如果id是唯一的,则将返回1个元素的集合,而不是对象本身,因此执行此操作时:

$matakuliahs->id
您正在尝试访问$matakuliahs对象的id属性,但在本例中,$matakuliahs不是一个对象,而是一个集合。 要解决此问题,您可以执行以下操作:
1.

获取对象并访问属性

2.在你看来:

@foreach( $matakuliahs as $matakuliah)
//your code here
@endforeach
希望这有帮助。谢谢你的方法

MyModel::find($id); 
或者与雄辩的关系在Laravel 4.2上不起作用只是下一个解决方案

$myModel= new MyModel;
$myModel->setConnection('mysql2');
$myModel= $myModel->where('id', $id)->first();
$matakuliahs->id
$matakuliahs = Matakuliah::where('id','=',$id)->get()->first(); 
$matakuliahs = Matakuliah::where('id','=',$id)->first(); 
@foreach( $matakuliahs as $matakuliah)
//your code here
@endforeach
MyModel::find($id); 
$myModel= new MyModel;
$myModel->setConnection('mysql2');
$myModel= $myModel->where('id', $id)->first();