laravel's request->string()
方法提供了一种简化的字符串操纵方法。它将输入数据转换为可弦的对象,从而使流利的方法链接以进行有效的转换。
// Basic transformation $name = $request->string('name') ->trim() ->title() ->limit(50); // Input: $request->input('name') = ' jANE mARY smith '; // Output: '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() ]); } }
>
以上是使用Laravel&#039; s String()方法简化字符串操纵的详细内容。更多信息请关注PHP中文网其他相关文章!