Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

External Service Class in Livewire

Premium
2 min read

In this lesson, let's see how you can call an external class from the Livewire component, like a Service class.

For example, you create a post, then send an email, updating other tables in the DB. So imagine this code has way more lines of code:

use App\Models\Post;
 
class PostForm extends Form
{
// ...
 
public function save(): void
{
Post::create($this->all());
 
$this->reset('title', 'body');
}
}

So we create some PostService where all the potential code for the post would go there.

app/Services/PostService.php:

class PostService
{
public function storePost(string $title, string $body): void
{
Post::create([
'title' => $title,
'body' => $body,
]);
}
}

Then you can use that service in a typical Laravel Controller. You can read about it in posts like Laravel Controller into Service Class with Injection or Service Classes in Laravel: All You Need to Know or check more here.

In Livewire we can call service the same way by creating a...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

P
peroxovy ✓ Link copied!

Can I call it like:

'app(PostService::class)->storePost($this->title, $this->body);` ?

What's the difference?

PK
Povilas Korop ✓ Link copied!

Yes you can. No difference, just different syntax, both work.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.