Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abhishekregmi's avatar

api returns html for image upload

I have made laravel project that works with both api and web, but the problem is api returns html page when i upload image to it My controller

public function store(Request $request)
    {
        $data = new Testimonial;
        $data->name = $request->name;
        $data->description = $request->description;
        $data->designation = $request->designation;
        $data->company = $request->company;
        if ($request->hasfile('image')) {
            $file = $request->file('image');
            $extenstion = $file->getClientOriginalExtension();
            $filename = time() . '.' . $extenstion;
            $file->move('uploads/testimonial/', $filename);
            $data->image = $filename;
        }
        $data->save();
        if (!request()->expectsJson()) {
            return \Redirect::to('/admin/testimonial/');
        } else {
            return response()->json([$data, 'message' => 'Success'], 200);
        }
    }
0 likes
3 replies
Sinnbeck's avatar

Can I suggest using controllers. Mixing a method like that isn't very good practice :)

Also, what html? The html from the /admin/testimonial page?

1 like
Sinnbeck's avatar

@abhishekregmi I would make 2 different controllers :) There can be a lot of differences between a web request and an api request. If they share code, you can always refactor to an action or a service.

And be sure to make a route for each. Laravel has an api.php file for api routes.

1 like

Please or to participate in this conversation.