Smsma's avatar
Level 2

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::getCollection does not exist.

I want to return faqs with translation using pagination

when I try this code that error appear

what is the solution to do this ??

public function getFaqs()
    {
        if (config('app.locale') === 'en') {

            $data = Faq::with('translations')
                        ->get(['id', 'question', 'answer'])
                        ->getCollection()
                        ->transform(function($faq, $key) {
                return [
                    'id' => $faq->id,
                    'question' => $faq->translate('en')->question,
                    'answer' => $faq->translate('en')->answer,
                ];
            });

            $data->paginate(10);

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

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

0 likes
6 replies
Yorki's avatar

Method get already returns Collection.

 $data = Faq::with('translations')
    ->get(['id', 'question', 'answer'])
    ->transform(function($faq, $key) {
        return [
            'id' => $faq->id,
            'question' => $faq->translate('en')->question,
            'answer' => $faq->translate('en')->answer,
        ];
    });
1 like
Smsma's avatar
Level 2

@Yorki , I want to use pagination with collection,how can I do this

Smsma's avatar
Level 2
$data = Faq::with('translations')
                        ->get(['id', 'question', 'answer'])
                        ->transform(function($faq, $key) {
                            return [
                                'id' => $faq->id,
                                'question' => $faq->translate('en')->question,
                                'answer' => $faq->translate('en')->answer,
                            ];
                        })->paginate(10);

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

Yorki's avatar
$data = Faq::with('translations')
    ->select(['id', 'question', 'answer'])
    ->paginate(20)
    ->transform(function($faq, $key) {
        return [
            'id' => $faq->id,
            'question' => $faq->translate('en')->question,
            'answer' => $faq->translate('en')->answer,
        ];
    });
1 like
Smsma's avatar
Level 2

how can I return this keys

     "first_page_url": "http://localhost/mertaah/api/ar/faqs?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://localhost/mertaah/api/ar/faqs?page=1",
        "next_page_url": null,
        "path": "http://localhost/mertaah/api/ar/faqs",
        "per_page": 10,
        "prev_page_url": null,
        "to": 3,
        "total": 3

Please or to participate in this conversation.