Search via API

Hi,

I registered a custom API endpoint to get search results from Statamic. The search works (I copied parts of it from the control panel SearchController).

But instead of returning the queried json object directly, I would like to return it as an EntryResource (with pagination data) like the Content API does for entries:

{
  "data": [
    {
      "title": "My First Day"
    }
  ],
  "links": {...},
  "meta": {...}
}

My controller looks like this:

class SearchController extends ApiController
{

        public function __invoke(Request $request)
    {
        $this->abortIfDisabled();

                return Search::index()
                    ->ensureExists()
                    ->search($request->input('term'))
                    ->get()
                    ->filter(function ($item) {
                        return $item->published();
                    })
                    ->map(function ($item) {
                        return $item->toAugmentedCollection()->withShallowNesting();
                    });
    }
}

How can I translate the output of Search into an EntryResource?

Thanks

Answered by Dominik Radl!
>>>>>>> Answered <<<<<<<
2 Replies
2 Followers