request->string()
Contoh ini menunjukkan penggunaan asas:
inilah aplikasi praktikal dalam sanitisasi data profil:
// Basic transformation $name = $request->string('name') ->trim() ->title() ->limit(50); // Input: $request->input('name') = ' jANE mARY smith '; // Output: 'Jane Mary Smith'
Kaedah
<?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() ]); } }
Atas ialah kandungan terperinci Melancarkan manipulasi rentetan dengan kaedah Laravel ' s (). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!