尝试从API获取数据并仍然使用Paginate但它不起作用。
我收到此错误: 调用数组
上的成员函数links()这是我的API控制器,我从中获取数据:
$positions = DB::table('position')
->join('company', 'position.company_id', '=', 'company.id')
->select('position.*', 'company.name')
->paginate(5);
if (!$positions) {
return response()->json(['message' =>'There is no positions', 'code' =>404], 404);
} else {
//echo "hit bby first";
return response()->json(['data' => $positions], 200);
}
这是通过JSON从API获取数据的控制器:
$jsonurl = "http://localhost/laravel.dev/index.php/positions";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
$newarray = json_decode(json_encode($json_output), True);
$data = array(
'positions' => $newarray['data'],
);
return view('mypage', $data);
这是在我的视图mypage中,我遍历代码
@foreach($positions['data'] as $position)
{{$position['title']}}
{{$position['location']}}
@endforeach
{{positions->links()}}
当我尝试在视图中回显它时,这就是数组的样子:
Array
(
[total] => 14
[per_page] => 5
[current_page] => 1
[last_page] => 3
[next_page_url] => http://localhost/laravel.dev/index.php/positions?page=2
[prev_page_url] =>
[from] => 1
[to] => 5
[data] => Array
(
[0] => Array
(
[id] => 1
[company_id] => 1
[title] => Software Developer
)
[1] => Array
(
[id] => 2
[company_id] => 2
[title] => Accountant
)