简体   繁体   English

未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id Laravel 4

[英]Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4

i'm using laravel v 4.2.. i want to create update record. 我正在使用laravel v 4.2 ..我想创建更新记录。 can you help me.. what's wrong with this code... this is my code : 你可以帮助我..这段代码有什么问题......这是我的代码:

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 edit.blade.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.. 感谢您的关注和帮助..

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();
$matakuliahs = Matakuliah::where('id','=',$id)->get(); 

return a colllection of object where the id is equal to $id. 返回id等于$ id的对象的colllection。 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是唯一的,那么将返回1个元素的集合而不是对象本身,所以当你这样做时:

$matakuliahs->id

you are triying to acess the id properties of the $matakuliahs object but the $matakuliahs in this case is not a object is a collection. 你正在试图获取$ matakuliahs对象的id属性,但在这种情况下$ matakuliahs不是一个对象是一个集合。 to solve this problem you can do: 要解决这个问题你可以做到:
1. 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 谢谢

Try this in your controller method: 在控制器方法中尝试这个:

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

And just pass it to the view. 然后将它传递给视图。

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id laravel 未定义属性:Illuminate\\Database\\Eloquent\\Collection::$id - laravel Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 错误 leftJoin:未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$id - Laravel Error leftJoin : Undefined property: Illuminate\Database\Eloquent\Collection::$id 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ name Laravel 5.3 - Undefined property: Illuminate\Database\Eloquent\Collection::$name Laravel 5.3 Laravel 5未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ basicdetails - Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails laravel 5.3:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection错误 - laravel 5.3 : Undefined property: Illuminate\Database\Eloquent\Collection Error Laravel关系错误:未定义属性:第1行上的Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel relationship error: Undefined property: Illuminate\Database\Eloquent\Collection::$id on line 1 未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$likes laravel - Undefined property: Illuminate\Database\Eloquent\Collection::$likes laravel 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: Laravel 5.2 - Undefined property: Illuminate\Database\Eloquent\Collection:: Laravel 5.2 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection - Undefined property: Illuminate\Database\Eloquent\Collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM