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

oliverbusk's avatar

(Small) actions with Inertiajs

Hi everyone.

I've been using Livewire quite extensively in the past, but I wanted to try out Inertia.js for a new project. I've been watching the Laracast series and lessons on how to get started with Inertia. So far, so good!

As I have previously been using Livewire, I've been creating small "actions"/"components" directly with PHP, which I think cuts down development time significantly.

For example, in Livewire I can quickly interact with the models directly in PHP (and other PHP classes for that matter):

Quick "pseudo-code"

//Livewire component:
public function mount(User $user){
	$this->user = $user;
}
public function createInvoice()
{
	if(! $this->user->canCreateInvoice()){
   		return false;
	}

   if(config('invoices.max_number_of_invoices') < $this->user->invoices->count()){
		return false;
	}

   return $this->user->invoices->create([...]);
}

How is this achieved with Inertia? Bear in mind, I have no experience in writing Vue.js apps/SPA.

0 likes
3 replies
Sinnbeck's avatar

You need to think of it in terms of how you would do it based on blade (kinda)

So imagine this being a controller method, that you post to

public function createInvoice(User $user) //user is found by route model binding
{
	if(! $user->canCreateInvoice()){
   		return redirect()->back(); //can be returned with an ERROR that you can render in vue/react
	}

   if(config('invoices.max_number_of_invoices') < $user->invoices->count()){
		return redirect()->back(); //can be returned with an ERROR that you can render in vue/react
	}

   $user->invoices->create([...]);
    return redirect()->back(); //can be returned with an SUCCESS message that you can render in vue/react
}
oliverbusk's avatar

@Sinnbeck I see - so these "actions" are moved to the controller?

Are there any best practices on how to organize this, as I like to keep my controllers thin.

Sinnbeck's avatar

@oliverbusk Personally my controllers (invokable) often call an action (take a look at JetStream as an example). But the return is done in the controller. You always need to return a response that inertia understand (redirect or an inertia response)

Please or to participate in this conversation.