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
留言分享你的想法!
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
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—分數計算
Thumbnail
中學數學基礎練習—一元一次方程式
Thumbnail
中學數學基礎練習—一元一次方程式
Thumbnail
本章節介紹了PHP中的各種運算符,包括算數運算子、比較運算子、賦值運算子和位元運算子。還討論了運算子的優先等級及其在表達式中的應用。了解這些運算符及其優先等級可以幫助編寫更高效和準確的PHP代碼。
Thumbnail
本章節介紹了PHP中的各種運算符,包括算數運算子、比較運算子、賦值運算子和位元運算子。還討論了運算子的優先等級及其在表達式中的應用。了解這些運算符及其優先等級可以幫助編寫更高效和準確的PHP代碼。
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—對數方程式
Thumbnail
高中數學主題練習—分點計算
Thumbnail
高中數學主題練習—分點計算
Thumbnail
高中數學主題練習—分點計算
Thumbnail
高中數學主題練習—分點計算
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News