laravel 相关问题

Laravel是一个免费的开源PHP Web框架,由Taylor Otwell创建,用于遵循模型 - 视图 - 控制器(MVC)架构模式并基于Symfony开发Web应用程序。 Laravel的源代码托管在GitHub上,并根据MIT许可条款获得许可。

Laravel:如何对请求使用多个验证规则

我有两个模型用户和配置文件一切工作正常并完美创建记录。 为了进行验证,我创建了如下请求类。 用户请求 我有两个模型User和Profile一切工作正常并完美创造记录。 为了验证,我创建了如下请求类。 用户请求 <?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Facades\Validator; use function __; use function preg_match; class UserRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool * @todo set role based permission for the method * */ public function authorize() { return TRUE; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { Validator::extend( 'without_spaces', function ($attr, $value) { return preg_match('/^\S*$/u', $value); }, __('validation.username_space') ); switch ($this->method()) { case 'POST': return [ 'username' => 'required|string|without_spaces|max:255|unique:users', 'email' => 'required|string|email|max:255|unique:users,email', 'password' => 'required|string|min:8|confirmed', 'role' => 'required', ]; case 'PUT': case 'PATCH': return [ 'username' => 'sometimes|required|string|without_spaces|max:255|unique:users,username,' . $this->user->id, 'email' => 'required|string|email|max:255|unique:users,email,' . $this->user->id, 'password' => 'nullable|string|min:8|confirmed', 'role' => 'required', ]; case 'GET': case 'DELETE': default: return []; break; } } } 个人资料请求 <?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; use function __; use function preg_match; class ProfileRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return FALSE; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { Validator::extend('unique_field', function ($attributes, $value, $parameters, $validator) { return; }, __('validation.username_space') ); switch ($this->method()) { case 'POST': case 'PUT': case 'PATCH': return [ 'first_name' => 'max:20', 'last_name' => 'max:20', 'mobile' => 'regex:/(01)[0-9]{9}/|digits:8 ', 'city' => 'max:30', 'facebook' => 'url', 'twitter' => 'url', 'youtube' => 'url', 'instagram' => 'url', ]; case 'GET': case 'DELETE': default: return []; break; } } } 用户控制器 public function store(UserRequest $request) { // begin transaction DB::beginTransaction(); try { // create user $user = User::create([ 'username' => $request->username, 'email' => $request->email, 'password' => Hash::make($request->password), 'role' => $request->role, ]); // set profile data $profile = new Profile([ 'first_name' => $request->first_name, 'last_name' => $request->last_name, 'mobile' => $request->mobile, 'city' => $request->city, 'facebook' => $request->facebook, 'twitter' => $request->twitter, 'youtube' => $request->youtube, 'instagram' => $request->instagram, ]); // save profile data $user->profile()->save($profile); // commit transaction DB::commit(); // if user created if ($user) { return redirect(route('admin.users.index'))->with('success', __('messages.admin.feedback.user_created')); } else { return redirect(route('admin.users.index'))->with('error', __('messages.admin.feedback.user_created')); } } catch (Throwable $exception) { // rollback if error DB::rollBack(); throw $exception; } } public function update(UserRequest $request, User $user) { try { $user->email = $request->email; $user->role = $request->role; if ($request->has('password')) { $user->password = Hash::make($request->password); } $user->save(); // set profile data $profile = [ 'first_name' => $request->first_name, 'last_name' => $request->last_name, 'mobile' => $request->mobile, 'city' => $request->city, 'facebook' => $request->facebook, 'twitter' => $request->twitter, 'youtube' => $request->youtube, 'instagram' => $request->instagram, ]; // save profile data $user->profile()->update($profile); return redirect(route('admin.users.index'))->with('success', __('messages.admin.feedback.user_updated')); } catch (Throwable $exception) { throw $exception; } } 问题: 我有一张用于用户字段和个人资料字段的表单。现在我不知道如何在方法中使用两个请求类来验证字段。 我找到了解决方法。在 ProfileRequest 和 store 方法中键入 update 类,并用它来验证配置文件字段。 用户控制器 public function store(UserRequest $request, ProfileRequest $profileRequest) { // begin transaction DB::beginTransaction(); try { // create user $user = User::create([ 'username' => $request->username, 'email' => $request->email, 'password' => Hash::make($request->password), 'role' => $request->role, ]); // set profile data $profile = new Profile([ 'first_name' => $profileRequest->first_name, 'last_name' => $profileRequest->last_name, 'mobile' => $profileRequest->mobile, 'city' => $profileRequest->city, 'facebook' => $profileRequest->facebook, 'twitter' => $profileRequest->twitter, 'youtube' => $profileRequest->youtube, 'instagram' => $profileRequest->instagram, ]); // save profile data $user->profile()->save($profile); // commit transaction DB::commit(); // if user created if ($user) { return redirect(route('admin.users.index'))->with('success', __('messages.admin.feedback.user_created')); } else { return redirect(route('admin.users.index'))->with('error', __('messages.admin.feedback.user_created')); } } catch (Throwable $exception) { // rollback if error DB::rollBack(); throw $exception; } } public function update(UserRequest $request, ProfileRequest $profileRequest, User $user) { try { $user->email = $request->email; $user->role = $request->role; if ($request->has('password')) { $user->password = Hash::make($request->password); } $user->save(); // set profile data $profile = [ 'first_name' => $profileRequest->first_name, 'last_name' => $profileRequest->last_name, 'mobile' => $profileRequest->mobile, 'city' => $profileRequest->city, 'facebook' => $profileRequest->facebook, 'twitter' => $profileRequest->twitter, 'youtube' => $profileRequest->youtube, 'instagram' => $profileRequest->instagram, ]; // save profile data $user->profile()->update($profile); return redirect(route('admin.users.index'))->with('success', __('messages.admin.feedback.user_updated')); } catch (Throwable $exception) { throw $exception; } } 在 UserRequest 类中,您可以将它们分配给一个数组,而不是直接从 switch 语句返回规则。为了我们的目的,我们称其为 $rules。然后您应该能够合并您的个人资料中的规则,返回合并的规则,如下所示: $profileRequest = new ProfileRequest(); // you may need to switch this to app()->make(ProfileRequest::class) if you get an error instantiating this return array_merge($rules, $profileRequest->rules());

回答 2 投票 0

Laravel 验证请求和 API 路由 POST 参数

我在表单请求验证以及如何使用一个 API 路由处理它方面遇到了一个小问题。 我需要创建的资源依赖于其他资源。 (这里 EmailSettings 属于

回答 3 投票 0

Laravel 中的 Ajax 页面加载问题

我在 Laravel 中使用 all() 方法获取数据并将其传递给视图,在视图中我通过 ajax 更新页面数据而不刷新页面,所以问题是页面在小...

回答 1 投票 0

当我尝试下载 spatie/laravel-medialibrary:^10.0.0 时,我收到一条消息

问题1 - 根composer.json需要spatie/laravel-medialibrary 10.0.0 -> 可满足spatie/laravel-medialibrary[10.0.0]。 - spatie/laravel-medialibrary 10.0.0 需要照亮...

回答 1 投票 0

Laravel 统计价格问题

我正在 Laravel 工作,但确实有一些帮助。我正在尝试查找打折产品的价格。 'totalPrice' => 1 - $request->input('discount')*$request->input('price'), ( 0.2 是折扣值...

回答 1 投票 0

Laravel Eloquent 截断 - 外键约束

我在使用 Laravel 5 删除数据时遇到一些问题。我似乎陷入了“外键约束”,但我不明白为什么。 在我当前的数据库模型中,我有一个数据点表,其中有一个

回答 3 投票 0

Laravel:替换嵌套路由中的中间件参数

我有一个路由组,它将中间件应用于所有嵌套路由。我正在向它传递一个参数。 不过,在组内,我有一条特定的路线,我想为其传递不同的参数。

回答 1 投票 0

Inertia <Head> 组件在标题中添加了“- Laravel”后缀

我使用 Laravel Inertia 和 React 作为我的应用程序的前端。当我使用 Inertia 的 组件在 React 组件中设置标题时,它会自动附加“-La...

回答 1 投票 0

如何构建 AlpineAJAX 响应

我正在尝试构建一个手风琴,其中列出了所有主要类别,当用户单击其中一个类别时,它应该展开以显示所有子类别。我有很多类别,每个类别...

回答 1 投票 0

在 Laravel 中下载 Cloudinary 文件

请问我有两个问题要问; 第一个是如何在 laravel 中下载 cloudinary 文件? 我已经尝试过chatGPT提供的所有方法。它下载文件(pdf),但文件大小......

回答 1 投票 0

jQuery 将 CSRF 令牌添加到所有 $.post() 请求的数据中

我正在开发一个 Laravel 5 应用程序,它默认为所有 POST 请求启用 CSRF 保护。我喜欢这种额外的安全性,所以我正在尝试使用它。 在发出简单的 $.post() 请求时,我

回答 8 投票 0

应用环境变量问题

我已将 APP_ENV=local 更改为 APP_ENV=Production,但是当我尝试从 \App::environment() 获取值时,它显示值“local”,我也清除了缓存 改变...

回答 1 投票 0

将 phpseclib 集成到 Laravel 5 中

我目前正在将我的项目从 Laravel 4 迁移到 Laravel 5,我仍然是 Laravel 和 OOP 的新手用户,但到目前为止一切都很顺利。 然而,在我的 L4 项目中,我使用 phpsec...

回答 3 投票 0

同时使用 Laravel Sanctum 和 Octane 时出现超时问题

我正在尝试迁移到 Laravel Octane。 我使用 Sanctum (React SPA) 中基于 cookie 的会话身份验证,并使用 Laravel 作为 API。 我尝试使用 Swoole 设置 Octane,这导致了很多

回答 1 投票 0

覆盖 Laravel 11 组件的默认属性

我是 Laravel 的新手,最近遇到了这个问题。我想覆盖 Laravel 11 组件的背景,但我只是添加一个未渲染的额外类,即

回答 1 投票 0

如何在 laravel statamic 中创建可重用的 html 块,以便我可以在 vuejs 中的不同页面上使用它?

我正在尝试使用 laravel statamic 和 vuejs 创建一个 CMS。我想要实现的是我想构建可重用的 html 块;例如,我想构建一个横幅图像块,管理员应该...

回答 1 投票 0

如何在Windows 10上升级php版本

我正在尝试让我的 Laravel 项目正常运行。但是当我使用 Composer update 时,它会显示以下内容: 该软件包需要 php >=5.6.4 但您的 PHP 版本(5.5.12)不满足该要求...

回答 6 投票 0

如何修复工厂中带有消息textfaker段落或文本的错误?

在 laravel/livewire 网站上,我使用 faker 段落方法使用消息文本制作工厂: $faker = \Faker\Factory::create(); 返回 [ 'user_id' => $this->faker->randomElement(User::all...

回答 1 投票 0

使用 Laravel 进行 Web 和 api 的多重身份验证

我有一个使用管理员表的网络管理面板,其中前端使用 api 和用户表进行身份验证 管理员和用户模型有些相同,只是表名不同 班级

回答 1 投票 0

Laravel 环境变量在 Google Cloud Run 中不起作用

我已经成功部署了太多次 Laravel 项目直至 Cloud Run,但现在 Cloud Run 似乎无法读取环境变量(我已经在变量中指定了...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.