Smsma's avatar
Level 2

pagination not work

in controller

public function getFaqs()
    {
        if (config('app.locale') === 'en') {
            $data = Faq::get();
          
            foreach ($data as $value) {
                return [
                    'id' => $value->id,
                    'question' => $value->translate('en')->question,
                    'answer' => $value->translate('en')->answer,
                ];
            }
            $data->paginate(2);

            return $this->respondWithSuccess($data);
        } else {
            $data = Faq::select('id', 'question', 'answer')->paginate(10);

            return $this->respondWithSuccess($data);
        }
    }

why pagination not work hear and it return the latest item only not all faq in database

0 likes
1 reply
Sergiu17's avatar
foreach ($data as $value) {
    return [
        'id' => $value->id,
        'question' => $value->translate('en')->question,
        'answer' => $value->translate('en')->answer,
    ];
}

No matter how many items $data contains, this loop will loop only one time. because you return :)

https://www.w3schools.com/php/default.asp :)

1 like

Please or to participate in this conversation.