laravel 未定义的属性:Illuminate\Database\Eloquent\Collection::$id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34020182/
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
提问by ken4ward
I don't know exactly why I'm getting this error as indicated by title. I am trying to write the code fro deleting records. This is the code below.
我不知道为什么我会收到标题所示的这个错误。我正在尝试编写用于删除记录的代码。这是下面的代码。
<TABLE >
@foreach($users as $user)
<TR><TD>{{ $user->id }}</TD><TD>{{ $user->firstname }}</TD><TD><div id="divContainer"><div class="theDiv"><form action="/users/{{ $users->id }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }}<button class="css-deletebutton">DELETE</button></form></div></div></TD><TD>{!! Form::submit('DEACTIVATE', ['class'=>'css-statusbutton']) !!}</TD></TR>
@endforeach
<!--{!! $users->render() !!}-->
</TABLE>
This is the route:
这是路线:
Route::resource('users', 'UserController');
This is the controller method:
这是控制器方法:
public function show($id)
{
$users = User::find($id);
return view('userpages.show')->with('users', $users);
}
This is the view to be displayed for deletion of any record.
这是为删除任何记录而显示的视图。
@section('body')
{!! Form::open([ 'method' => 'delete', 'route' => ['users.destroy', $users->id]]) !!}
<TABLE>
<TR><TD>{{ $users->firstname }}</TD><TD>{{ $users->lastname }}</TD><TD>{{ $users->email }}</TD><TD>{{ $users->username }}</TD><TD>{!! Form::submit('DELETE', ['class'=>'css-deletebutton']) !!}</TD></TR>
</TABLE>
{!! Form::close() !!}
@stop
This is the error message I'm getting:
这是我收到的错误消息:
ErrorException in acf1a7ad2174bd2b743200b1a50b4c9f line 14:
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\Users\ken4ward\Documents\xampp\htdocs\schoolproject\resources\views\userpages\index.blade.php)
回答by jedrzej.kurylo
I can see you are referring to $users->idwithin the foreachloop in the first code snippet - that's what's causing the error you're getting. $usersis the variable that holds the collection and $useris the variable that you should use to access user's data, including user's ID.
我可以看到您在第一个代码片段的foreach循环中指的是$users->id- 这就是导致您遇到错误的原因。$users是保存集合的变量,$user是您应该用来访问用户数据(包括用户 ID)的变量。