php 未定义的属性:Illuminate\Database\Eloquent\Collection::$id Laravel 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26085888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4
提问by Febry Damatraseta Fairuz
i'm using laravel v 4.2.. i want to create update record. can you help me.. what's wrong with this code... this is my code :
我正在使用 laravel v 4.2 .. 我想创建更新记录。你能帮我..这段代码有什么问题......这是我的代码:
MatakuliahsController.php
MatakuliahsController.php
public function edit($id) { //$matakuliahs = $this->matakuliahs->find($id); $matakuliahs = Matakuliah::where('id','=',$id)->get(); if(is_null($matakuliahs)){ return Redirect::route('matakuliahs.index'); } return View::make('matakuliahs.edit',compact('matakuliahs')); }
edit.blade.php
编辑刀片.php
{{ Form::open(array('autocomplete' => 'off', 'method' => 'PATCH', 'route' => array('matakuliahs.update', $matakuliahs->id))) }} ... {{ Form::close() }}
Error is :
错误是:
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\xampp\htdocs\Laravel 4\projectLaravel\app\views\matakuliahs\edit.blade.php)
thanks for your attention and your help..
感谢您的关注和帮助..
回答by KAS
What you are trying to get is a relationship on a collection of models, the relationship exists on the object in that collection. You can use first() to return the first one or you need to use loop for each one get their items
您试图获得的是模型集合上的关系,该关系存在于该集合中的对象上。您可以使用 first() 返回第一个,或者您需要为每个人使用循环获取他们的项目
$matakuliahs = Matakuliah::where('id','=',$id)->get()->first();
回答by Ornelio Chauque
$matakuliahs = Matakuliah::where('id','=',$id)->get();
return a colllection of object where the id is equal to $id. in this case will return a collection of 1 element and not the object itself of course if the id is unique, so when you do:
返回 id 等于 $id 的对象集合。在这种情况下,如果 id 是唯一的,当然会返回 1 个元素的集合而不是对象本身,所以当你这样做时:
$matakuliahs->id
you are triying to acess the id properties of the $matakuliahsobject but the $matakuliahsin this case is not a object is a collection.
to solve this problem you can do:
1.
您正在尝试访问$matakuliahs对象的 id 属性, 但在这种情况下$matakuliahs不是对象而是集合。要解决此问题,您可以执行以下操作:
1。
$matakuliahs = Matakuliah::where('id','=',$id)->get()->first();
or
或者
$matakuliahs = Matakuliah::where('id','=',$id)->first();
to get the object and acess the properties.
2. on you view:
获取对象并访问属性。
2.关于你的看法:
@foreach( $matakuliahs as $matakuliah)
//your code here
@endforeach
hope this help. thanks
希望这有帮助。谢谢
回答by jrodriguez
The method
方法
MyModel::find($id);
Or relationship with Eloquent no works on Laravel 4.2 just next solution
或者与 Eloquent 的关系在 Laravel 4.2 上不起作用,只是下一个解决方案
$myModel= new MyModel;
$myModel->setConnection('mysql2');
$myModel= $myModel->where('id', $id)->first();
回答by R_g
Try this in your controller method:
在您的控制器方法中试试这个:
$matakuliahs = Matakuliah::find($id);
And just pass it to the view.
并将其传递给视图。