在 Laravel 表单中创建的特征 无法在另一个类中调用请求文件



我刚刚在Laravel表单请求文件中创建了PHP Trait,与Laravel表单请求类一起。这样做的目的是让我可以在laravel livewire组件类中使用这个特性。但是它抛出了一个错误

特征"App Http请求 Auth LoginTrait"没有找到

这与laravel psr自动加载有关还是什么?

我LoginRequest.php:

<?php
namespace AppHttpRequestsAuth;
use IlluminateFoundationHttpFormRequest;
class LoginRequest extends FormRequest
{
use LoginTrait;
}
trait LoginTrait
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules($data = null)
{
$phoneRule = getPhoneRule();
$rule = isEmail($this->phone_email) ? 'email:rfc,dns' : "phone:$phoneRule," . 
getCountryCode();
return [
'phone_email' => "required|$rule",
'password' => 'required|min:6'
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes($data = null)
{
$attributes = [
'phone_email' => 'Email/Whatsapp'
];
if (isEmail($this->phone_email)) {
$attributes['phone_email'] = 'Email';
} else if (isPhoneNumber($this->phone_email)) {
$attributes['phone_email'] = 'Whatsapp';
}
return $attributes;
}
}

我的LoginForm livewire组件类:

<?php
namespace AppViewComponentsLivewireForms;
use AppHttpRequestsAuthLoginTrait;
use LivewireComponent;
class LoginForm extends Component
{
use LoginTrait;
public $phone_email;
public $password;
protected $validationAttributes;
// protected $validationAttributes;
public function render()
{
return view('components.livewire.forms.login-form');
}
public function updated($propertyName)
{
$this->validationAttributes = $this->attributes();
$this->validateOnly($propertyName);
}
public function mount()
{
$this->fill([
'phone_email' => old('phone_email')
]);
}
}

回答我的问题,为什么我们不能声明类和Trait在同一个文件(laravel)?

您的问题可能与LiveWire不与Laravel的FormRequest一起工作有关吗?

相关内容

最新更新