Is it possible to use API Resource to return JSONP?

Posted 3 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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.