lundi 2 mai 2016

paginate json API Laravel 5 Illegal string offset

Trying to get data from an API and still use the Paginate but it does not work.

This is get this error: Illegal string offset 'title'

This is my API controller that where i get the data from:

$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);
}

This is the controller which gets data from the API through JSON:

$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);

This is in my view mypage where i loop through the code

@foreach($positions as $position)

    
    

@endforeach


And this is what the array looks like when i try to echo it in the view:

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

                )



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire