我在控制器中有这个功能:
public function index()
{
$call=45;
$show = DB::select('select * from users where position="keeper" ');
return View('index',['users'=>$show,'call'=>$call]);
}
在视图中我有这段代码:
Call: {{$call}}
Position: {{$users->position}} //what is the wrong with this line? everything is fine if i remove this line.
@foreach ($users as $u)
{{$u->id}}
{{$u->name}}
{{$u->email}}
@endif
错误:尝试获取非对象的属性
同样适用于循环:
@foreach($users as $r)
{{$r->position}}
@endforeach
答案 0 :(得分:0)
$ show = DB :: select('select * from user where position =“keeper”');
此查询返回多维对象。这就是为什么你不能像$user->position
那样访问它的属性。
要解决此问题,请尝试以下操作:
public function index()
{
$call=45;
$show = DB::select('select * from user where position = "keeper" limit 1')[0];
return View('index', ['user' => $show, 'call' => $call]);
}
答案 1 :(得分:0)
你可以这样做..
public function index()
{
$call=45;
$show = DB::table('user')->where("position", "keeper")->first();
return View('index',['user'=>$show,'call'=>$call]);
}