Ray Lee | 李宗叡
Learn or Die
Published in
1 min readJun 11, 2024

--

# Anonymous Event Broadcasting

11.5 新增了 anonymous event,即如果確定這一個 event 就只有當下會用到,它不會也不該在其他地方被使用,那就可以使用 anonymous event

直接使用 send()

<?php
Broadcast::on('orders.'.$order->id)->send();

這會 dispatch 一個 anonymous event,前端會收到如下資料

{
"event": "AnonymousEvent",
"data": "[]",
"channel": "orders.1"
}

也可以使用 as & with 來定義 event name & data

<?php
Broadcast::on('orders.'.$order->id)
->as('OrderPlaced')
->with($order)
->send();

前端會收到如下資料

{
"event": "OrderPlaced",
"data": "{ id: 1, total: 100 }",
"channel": "orders.1"
}

可以使用 private() or present() 來 dispatch anonymous event 到 private or present channel

<?php
Broadcast::private('orders.'.$order->id)->send();
Broadcast::presence('channels.'.$channel->id)->send();

send() 會將 event dispatch 到 Queue,如果想要立即 dispatch event,可以使用 sendNow()

<?php
Broadcast::on('orders.'.$order->id)->sendNow();

可以使用 toOthers(),只將 event dispatch 給 authenticated user 之外的人

<?php
Broadcast::on('orders.'.$order->id)
->toOthers()
->send();

# Blade Performance Improvements

11.5 針對 Blade rendering 做了優化,官宣效果有 20%,細節可參考文件

# Ability to Generate URLs With Query Params

11.5 可以產生 URL with query parameters

<?php
// http://localhost/products?sort=-name
url()->query('products', ['sort' => '-name']);

// http://localhost/products?columns[0]=name&columns[1]=price&columns[2]=quantity
url()->query('products', ['columns' => ['name', 'price', 'quantity']]);

// Overiding parameters:
// http://localhost/products?sort=-price
url()->query('products?sort=-name', ['sort' => '-price']);

// Appending parameters
// http://localhost/products?sort=-name&search=samsung
url()->query('products?sort=-name', ['search' => 'samsung']);

# Add a Default Namespace for make:trait and make:interface

11.5 開始,Artisan command make:trait & make:interface,會根據帶入的 path 產生對應的 Namespace

# 參考來源

Laravel News

--

--

Ray Lee | 李宗叡
Learn or Die

It's Ray. I do both backend and frontend, but more focus on backend. I like coding, and would like to see the whole picture of a product.