I have this function in controller:
public function index()
{
$call=45;
$show = DB::select('select * from users where position="keeper" ');
return View('index',['users'=>$show,'call'=>$call]);
}
In the view i have this piece of code:
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
Error: Trying to get property of non-object
same works well with loop:
@foreach($users as $r)
{{$r->position}}
@endforeach
$show = DB::select('select * from user where position="keeper" ');
this query returns multi-dimensional object. That's why you can not access it's property like this $user->position
.
To fix the issue, try like this:
public function index()
{
$call=45;
$show = DB::select('select * from user where position = "keeper" limit 1')[0];
return View('index', ['user' => $show, 'call' => $call]);
}
You can do this way..
public function index()
{
$call=45;
$show = DB::table('user')->where("position", "keeper")->first();
return View('index',['user'=>$show,'call'=>$call]);
}