aceraven777
aceraven777

Reputation: 4556

Can I use route model binding in channel routes?

I want to use route model binding in channel route via customizing the key (https://laravel.com/docs/10.x/routing#customizing-the-key). I've created a channel in route:

Broadcast::channel("users.{product:product_number}", function (User $user, Product $product) {
    return true;
});

But there's an error here:

preg_match(): Compilation failed: syntax error in subpattern name (missing terminator?) at offset 26 {"userId":107,"exception":"[object] (ErrorException(code: 0): preg_match(): Compilation failed: syntax error in subpattern name (missing terminator?) at offset 26

But when I use this syntax:

Broadcast::channel("users.{product}", function (User $user, Product $product) {
    return true;
});

The above code works, but I don't want the channel name to be users.{product.id}, I want it to be users.{product.product_number}.

I know I can do it this way:

Broadcast::channel("users.{productNumber}", function (User $user, $productNumber) {
    $product = Product::whereProductNumber($productNumber)->first();

    return true;
});

But I want to know if there's a way to do it via route model binding.

Upvotes: 1

Views: 38

Answers (0)

Related Questions