我想分頁聯合查詢結果。我寫了這個:無法訪問分頁聯合項的查詢結果
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
刀片template.Suppose我寫這篇訪問結果:
<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
從上面coeds返回此:
{
"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": "[email protected]",
},
{
"id": 7,
"first_name": "hossein",
"last_name": "rezaei",
"created_at": "2012-04-16 22:11:46",
"email": "[email protected]",
},
]
}
我試圖$data->data
訪問的結果,但同樣的錯誤occures。
更新2:
我發現,當我加入{{dd($user)}}
這樣的:
<div class="container">
@foreach ($data as $user)
{{dd($user)}}
{{ $user->first_name }}
@endforeach
</div>
返回一個包含array
而不是結果object
。
您選擇了first_name和last_name,但您試圖顯示'$ user-> name'? – SteD
我嘗試,但同樣的問題發生。 –
'var_dump($ data)'並檢查它是否沒有添加新的深度級別。 –