Search code examples
phplaravel-livewire

Laravel Volt routes


How do I get a CategoryID in livewire?

In the usual Route::get it worked - "public function createTopic($categoryId): void", how to do it here?

Volt::route('forum/cat{categoryId}/create', 'pages.forum.topic') ->name('forum.topic');


new #[Layout('layouts.app')] class extends Component {
    public string $name = '';

    public function createTopic(): void
    {
        $validated = $this->validate([
            'name' => ['required', 'string', 'min:6', 'max:255']
        ]);

        $topic = ForumTopicModel::create([
            'name' => $validated['name'],
            'category' => $categoryId,
            'author' => Auth::id()
        ]);
?>

Solution

  • This is done in the same way.

    In routes:

    Volt::route('forum/cat/{categoryId}/create', 'pages.forum.topic')->name('forum.topic');
    

    In the component:

    public int $categoryId;
    

    In component methods we use

    $this->categoryId;
    

    Also via mount:

    mount(function (int $categoryId) {
    
    });