تبسيط معالجة السلاسل النصية باستخدام دالة string() في لارافيل.

عند استخدام `request()->string()` في لارافيل المدخلات إلى مثيلات من نوع `Stringable`، مما يوفر إمكانيات سهلة وسلسلة لمعالجة السلاسل النصية من خلال method chaining.
// Basic transformation
$name = $request->string('name')
->trim()
->title()
->limit(50);
 
// Input
$request->input('name') = ' jANE mARY smith ';
 
// Output after string() transformation
'Jane Mary Smith'
 مثال آخر على تطهير بيانات الملف الشخصي:
<?php
 
namespace App\Http\Controllers;
 
use App\Models\Profile;
use Illuminate\Http\Request;
 
class ProfileController extends Controller
{
public function update(Request $request, Profile $profile)
{
$profile->update([
'display_name' => $request->string('name')
->trim()
->title()
->limit(50)
->toString(),
 
'username' => $request->string('username')
->trim()
->lower()
->replaceMatches('/[^a-z0-9_-]/', '')
->limit(20)
->toString(),
 
'bio' => $request->string('bio')
->trim()
->stripTags()
->limit(160)
->toString(),
 
'website' => $request->string('website')
->trim()
->lower()
->replace(['http://', 'https://'], '')
->before('/')
->toString()
]);
 
return response()->json([
'message' => 'Profile updated successfully',
'profile' => $profile->fresh()
]);
}
}
تقوم `string()` بعملية تطهير وتغيير المدخلات من خلال ربط السلس، مما يجعل معالجة السلاسل النصية أكثر وضوحًا وقابلية للصيانة.
جميع الحقوق محفوظة © 2025 Laravel | عربي