laravel 如何从模型中获取除一个以外的所有数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42732632/
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
How to get All data from a model except one?
提问by Hola
I want to retrieve all users data except one.for that i used the following query
我想检索除一个之外的所有用户数据。为此我使用了以下查询
$users=User::whereNotIn('name',['admin'])->pluck('id','name');
When i dd() the output I see all the users data except the one But when I send the query results in foreach()
loop in view page I see
当我 dd() 输出时,我看到除一个之外的所有用户数据但是当我foreach()
在视图页面中循环发送查询结果时,我看到
Trying to get property of non-object
Trying to get property of non-object
Error in view page.What's the Error here? Can anyone suggest me please?
视图页面出错。这里的错误是什么?有人可以建议我吗?
Here is the foreach loop i used in view page
这是我在视图页面中使用的 foreach 循环
@foreach($users as $user)
<option value="{{$user->id}}">{{$user->name}}</option>
@endforeach
采纳答案by Alexey Mezenin
pluck()
creates an array with [id => name]
structure, so change the code in assign_rol??e.blade.php
to:
pluck()
创建一个具有[id => name]
结构的数组,因此将代码更改assign_rol??e.blade.php
为:
@foreach ($users as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
@endforeach
And pluck()
parameters to:
和pluck()
参数:
->pluck('name', 'id');
回答by Chandhru Sekar
$users = User::where('id', '!=', Auth::id())->get();
this one except current login user..you can set admin id...in such field.. or you can use this also..
除了当前登录用户之外的这个......你可以设置管理员ID......在这样的领域......或者你也可以使用它......
$users = User::all()->except(Auth::id());
both are work!!
两个都是工作!!