我想分页联盟查询结果。我写了这个:
use Illuminate\Support\Facades\Input;
use Illuminate\Pagination\Paginator;
$page = Input::get('page', 1);
$paginate = 10;
$members = DB::table("members")
->select("id", "first_name", "last_name", "email", "created_at")
->where("site_id", $id);
$users = DB::table("users")
->select("id", "first_name", "last_name", "email", "created_at")
->where("site_id", $id)
->union($members)
->get()->toArray()
$slice = array_slice($users, $paginate * ($page - 1), $paginate);
$data = new Paginator($slice, $paginate);
return View::make('main.pages.search',compact('data','searchTerm'));
现在我想在search
刀片模板中访问结果。假设我写了这个:
<div class="container">
@foreach ($data as $user)
{{ $user->first_name }}
@endforeach
</div>
{{ $data->links() }}
但我收到了这个错误:
Trying to get property of non-object (View: D:\wamp\www\aids\resources\views\main\pages\search.blade.php
无法识别$user
及name
属性
有什么问题?
更新
而来自上面的return $data
来自这个:
{
"per_page": 2,
"current_page": 1,
"next_page_url": null,
"prev_page_url": null,
"from": 1,
"to": 2,
"data": [
{
"id": 6,
"first_name": "ali",
"last_name": "hassani",
"created_at": "2012-04-16 22:11:46",
"email": "ali@gmail.com",
},
{
"id": 7,
"first_name": "hossein",
"last_name": "rezaei",
"created_at": "2012-04-16 22:11:46",
"email": "reza@gmail.com",
},
]
}
我尝试$data->data
访问结果但发生了同样的错误。
更新2:
我发现当我添加{{dd($user)}}
时这样:
<div class="container">
@foreach ($data as $user)
{{dd($user)}}
{{ $user->first_name }}
@endforeach
</div>
返回array
包含结果而不是object
。