Is it possible to use API Resource to return JSONP?

Posted 5 months ago by malhayek

I am trying to use jquery.ui autocomplete https://jqueryui.com/autocomplete/#remote in my Laravel 5.7 project.

It seems that the jquery.ui.autocomplete package expect jsonp not standard json. I have been creating a new ApiResource for all of my resources, so I would like to also use Api Resource to response to the autocomplete request.

How can I change the response type from json to jsonp when using Api Resources?

Here is home my controller looks like

class DriversController extends Controller
{
    /**
     * Display the specified reservation.
     *
     * @param int $vehicleId
     *
     * @return App\Resources\VehicleResources
     */
    public function find($term)
    {
        $topics = Topic::where('fullname', 'like', $term. '%')->get();

        return new TopicResource($topics);
    }
}

Here is my ApiResponse class

class TopicResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            // return something cool
        ];
    }

    /**
     * Returns other attributes to the request
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function with($request)
    {
        return [
            'status' => 'success',
        ];
    }
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.