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

mcadio's avatar

Use same controller for Blade and Vue?

I've had a Laravel app for a while, but I recently added Vue 3 to the app for an admin dashboard. The Dash uses Vue, with values passed from blade.

All the other features on the admin side are done without Vue. So, the responses return a view, or return back to the previous page with messages.

The challenge: I have a feature I call Quick schedule links, that will book someone's appointment if it is during office hours, no one else is scheduled in that spot, and if the spot is booked by someone in that family it is only available until 2 members are in that spot.

I also have an Add->Appt section in the Menu (uses Blade), and my admin schedule allows booking open appts (uses Blade) - these both point to the same form that submits to the same function.

Is there a way to point my Vue scheduling feature to the SAME method I'm using on the other pages, but return a JSON response instead?

It seems like a huge waste to write almost the exact same function but for Vue. Or should I just duplicate it and change the responses to JSON?

My guess, now that I'm trying to clean things up, would be to pass a value that by default is null to the same function:

public function bookIfAvailable($request, $json = null) {
// do a bunch of stuff

	if($json === true) {
		return->response()->json([
			'appts' => $appts
		])
	} else {
		return redirect()->route('home',['appts'=>$appts,'failed'=>$failed]);
	}
}

Could this work?

0 likes
4 replies
Tray2's avatar
Tray2
Best Answer
Level 73

You can do that but I would use api routes for vue instead.

If you have the same queries in both you can push the into the models.

1 like
mcadio's avatar

Thanks. I did end up getting both to work by handing an extra parameter in the request for json. Just saved myself a ton of code. When I learn more about api routes vs the others, I’ll set that up.

piljac1's avatar

@mcadio Late to the party, but you don't need to pass a parameter. You can simply add an Accept header with the value application/json. You can then use $request->expectsJson() in your controller to determine if you should return JSON or not.

2 likes
mcadio's avatar

@piljac1 this is cool. I noticed the header request has content-type: application/json. I'm assuming this is the same no matter whether it's coming from blade or vue. I'll look into how to add that header with axios. Thanks!

Please or to participate in this conversation.