Laravel 週報 12.15

更新 發佈閱讀 7 分鐘

# Locale-aware Number Parsing

12.15 新增 Number class 新增 method,支援 parse with different locale

<?php
use Illuminate\Support\Number;

Number::parse($string);
Number::parseInt($string);
Number::parseFloat($string);

// Pass locale
Number::parseFloat(string: $string, locale: 'de');

// It is also possible to pass the type to the parse method
Number::parse(
string: $string,
type: NumberFormatter::TYPE_INT64,
locale: 'de',
);

# Default Option When Retrieving an Enum

12.15,request()->enum() 支援 default value

<?php
// Before
$chartType = request()->enum('chart_type', ChartTypeEnum::class) ?: ChartTypeEnum::Bar;

// After
$chartType = request()->enum('chart_type', ChartTypeEnum::class, ChartTypeEnum::Bar);

# TestResponse method to Assert a Client Error

12.15,TestResponse 新增 assertClientError()

<?php
// Before
$this->assertTrue($response->isClientError());

// After
$response->assertClientError();

# String hash Helper Method

12.15 Stringable 新增 hash() method

<?php
// Before you would pipe the string to the hash method manually
$appId = str('secret-unique-key')
->pipe(fn(Stringable $str) => hash('xxh3', (string) $str))
->take(10)
->prepend('app-');

$appId = str('secret-unique-key')
->hash('xxh3')
->take(10)
->prepend('app-');
// app-0fb8aac18c

# Add current_page_url to the Paginator

12.15 新增 current_page_url to Paginator

<?php
$paginator->toArray()['current_page_url'];

# Assert Redirect to Action

12.15 新增 assertRedirectToAction() action

<?php
$this->get('redirect-to-index')
->assertRedirectToAction([TestActionController::class, 'index']);

# Use Context Values as a Contexual Attribute

12.15 支援以 attribute 的方式來取得 context attribute

<?php
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Context;

class UserSeeder extends Seeder
{
public function run(): void
{
$user = User::factory()->create();

// Store context data for subsequent seeders...
Context::add('user', $user);
}
}

//Reuse the `$user` context in the ChannelSeeder
use App\Models\Channel;
use App\Models\User;
use Illuminate\Container\Attributes\Context;
use Illuminate\Database\Seeder;

class ChannelSeeder extends Seeder
{
public function run(
// Retrieve context data with added type-hinting...
#[Context('user')] User $user,
): void {
$channel = Channel::factory()->create();

$channel->users()->attach($user);
}
}

# 參考來源

Laravel News

留言
avatar-img
Learn or Die
0會員
16內容數
分享程式技術
Learn or Die的其他內容
2025/06/04
# Arr::from() Method 12.14 新增 Arr::from(),可從多種資料結構取得 array of items <?php use Illuminate\Support\Arr; Arr::from(collect(['name' => 'Laravel'])); /
2025/06/04
# Arr::from() Method 12.14 新增 Arr::from(),可從多種資料結構取得 array of items <?php use Illuminate\Support\Arr; Arr::from(collect(['name' => 'Laravel'])); /
2025/05/28
# 安裝 npm install -g typescript # 編譯 使用框架如 NextJS 時,框架會自動執行 TS 編譯 tsc hello.ts # Hello TypeScript 範例:function sayHello(person: string) { retu
2025/05/28
# 安裝 npm install -g typescript # 編譯 使用框架如 NextJS 時,框架會自動執行 TS 編譯 tsc hello.ts # Hello TypeScript 範例:function sayHello(person: string) { retu
2025/05/26
# JSON Unicode Cast Type 12.3 Eloquent Attributes 新增 json:unicode cast type,允許 JSON encoding JSON_UNESCAPED_UNICODE <?php protected $casts = [ '
2025/05/26
# JSON Unicode Cast Type 12.3 Eloquent Attributes 新增 json:unicode cast type,允許 JSON encoding JSON_UNESCAPED_UNICODE <?php protected $casts = [ '
看更多
你可能也想看
Thumbnail
本文分析導演巴里・柯斯基(Barrie Kosky)如何運用極簡的舞臺配置,將布萊希特(Bertolt Brecht)的「疏離效果」轉化為視覺奇觀與黑色幽默,探討《三便士歌劇》在當代劇場中的新詮釋,並藉由舞臺、燈光、服裝、音樂等多方面,分析該作如何在保留批判核心的同時,觸及觀眾的觀看位置與人性幽微。
Thumbnail
本文分析導演巴里・柯斯基(Barrie Kosky)如何運用極簡的舞臺配置,將布萊希特(Bertolt Brecht)的「疏離效果」轉化為視覺奇觀與黑色幽默,探討《三便士歌劇》在當代劇場中的新詮釋,並藉由舞臺、燈光、服裝、音樂等多方面,分析該作如何在保留批判核心的同時,觸及觀眾的觀看位置與人性幽微。
Thumbnail
高中數學主題練習—分點計算
Thumbnail
高中數學主題練習—分點計算
Thumbnail
這是一場修復文化與重建精神的儀式,觀眾不需要完全看懂《遊林驚夢:巧遇Hagay》,但你能感受心與土地團聚的渴望,也不急著在此處釐清或定義什麼,但你的在場感受,就是一條線索,關於如何找著自己的路徑、自己的聲音。
Thumbnail
這是一場修復文化與重建精神的儀式,觀眾不需要完全看懂《遊林驚夢:巧遇Hagay》,但你能感受心與土地團聚的渴望,也不急著在此處釐清或定義什麼,但你的在場感受,就是一條線索,關於如何找著自己的路徑、自己的聲音。
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
本章節介紹了PHP中的各種運算符,包括算數運算子、比較運算子、賦值運算子和位元運算子。還討論了運算子的優先等級及其在表達式中的應用。了解這些運算符及其優先等級可以幫助編寫更高效和準確的PHP代碼。
Thumbnail
本章節介紹了PHP中的各種運算符,包括算數運算子、比較運算子、賦值運算子和位元運算子。還討論了運算子的優先等級及其在表達式中的應用。了解這些運算符及其優先等級可以幫助編寫更高效和準確的PHP代碼。
Thumbnail
《轉轉生》(Re:INCARNATION)為奈及利亞編舞家庫德斯.奧尼奎庫與 Q 舞團創作的當代舞蹈作品,結合拉各斯街頭節奏、Afrobeat/Afrobeats、以及約魯巴宇宙觀的非線性時間,建構出關於輪迴的「誕生—死亡—重生」儀式結構。本文將從約魯巴哲學概念出發,解析其去殖民的身體政治。
Thumbnail
《轉轉生》(Re:INCARNATION)為奈及利亞編舞家庫德斯.奧尼奎庫與 Q 舞團創作的當代舞蹈作品,結合拉各斯街頭節奏、Afrobeat/Afrobeats、以及約魯巴宇宙觀的非線性時間,建構出關於輪迴的「誕生—死亡—重生」儀式結構。本文將從約魯巴哲學概念出發,解析其去殖民的身體政治。
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
高中數學主題練習—分點計算
Thumbnail
高中數學主題練習—分點計算
Thumbnail
背景:從冷門配角到市場主線,算力與電力被重新定價   小P從2008進入股市,每一個時期的投資亮點都不同,記得2009蘋果手機剛上市,當時蘋果只要在媒體上提到哪一間供應鏈,隔天股價就有驚人的表現,當時光學鏡頭非常熱門,因為手機第一次搭上鏡頭可以拍照,也造就傳統相機廠的殞落,如今手機已經全面普及,題
Thumbnail
背景:從冷門配角到市場主線,算力與電力被重新定價   小P從2008進入股市,每一個時期的投資亮點都不同,記得2009蘋果手機剛上市,當時蘋果只要在媒體上提到哪一間供應鏈,隔天股價就有驚人的表現,當時光學鏡頭非常熱門,因為手機第一次搭上鏡頭可以拍照,也造就傳統相機廠的殞落,如今手機已經全面普及,題
Thumbnail
中學數學基礎練習—一元一次方程式
Thumbnail
中學數學基礎練習—一元一次方程式
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News