jeudi 28 mars 2019

How to fix laravel eloquent and DB returning empty items(models) object

I noticed this problem started when I wanted to generate pagination for listStory() method inside a controller.When I investigated I discovered the #items is empty which was not initially. It is calculating the total correctly

During my investigation I tried

$allStoryByTeller = Story::all();
dd($allStoryByTeller);

and it returns

Collection {#344 ▼
  #items: array:10 [▼
    0 => Story {#345 ▶}
    1 => Story {#346 ▶}
    2 => Story {#347 ▶}
    3 => Story {#348 ▶}
    4 => Story {#349 ▶}
    5 => Story {#350 ▶}
    6 => Story {#351 ▶}
    7 => Story {#352 ▶}
    8 => Story {#353 ▶}
    9 => Story {#354 ▶}
  ]
}

which should be what the #items in the below output contains


    public function listStory()
    {
        $allStoryByTeller = Story::with([
            'user'
        ])->paginate($perPageCount = 10);
        dd($allStoryByTeller);
        ...
    }


and this snippet above returns

LengthAwarePaginator {#316 ▼
  #total: 9
  #lastPage: 1
  #items: Collection {#318 ▼
    #items: []
  }
  #perPage: 10
  #currentPage: 2
  #path: "http://127.0.0.1:8000/v1/stories/list"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▶]
}

I expected the #items to contain 9 models instead of being [] empty as the total is correctly calculated



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire