我有一个方法:
protected function validateLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|string|exists:users,email,active,1,online,0',
'password' => 'required|string',
], [
'password.required' => 'Password required!',
'email.exists' => 'Email not found!',
]);
}
验证“用户”表中的数据库中是否存在具有特定电子邮件地址的用户,该用户处于活动状态且未登录。 “用户”表的架构如下:
Schema::create($this->table, function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('active')->default(true);
$table->boolean('online')->default(false);
$table->timestamps();
});
我想根据用户状态(在线|活动)返回错误消息
你能告诉我我该怎么做吗? 我尝试过:
protected function validateLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|string|exists:users,email,active,1,online,0',
'password' => 'required|string',
], [
'password.required' => 'Password required!',
'email.exists' => 'Email not found!',
'email.active' => 'User not active!',
'email.online' => 'Email is onlone!',
]);
}
答案 0 :(得分:0)
我真的不知道exists:users,email,active,1,online,0
是否有效。我将从数据库中获取活动或在线字段,然后相应地返回一条消息。
答案 1 :(得分:0)
首先,您需要提供FormRequest的解决方案,如以下代码段所示:
namespace App\Http\Requests;
use Illuminate\Support\Facades\Auth;
/**
* Class UserRequest
* @package App\Http\Requests
*/
class UserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return !Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|unique:users|email_active:email@me.com|email_online:email@me.com',
'password' => 'required|string|between:4,40'
];
}
public function messages()
{
return [
'email.required' => 'Email is required',
'email.unique' => 'This Email is already in use',
'email.email_active' => 'This email is not active',
'email.email_online' => 'This email is online',
'password.required' => 'Password is required',
'password.between' => 'Password length should be between :min and :max characters'
];
}
}
email_active和email_online是两个不适用于Laravel框的规则。您应该通过php artisan make:rule EmailActiveRule和php artisan make:rule EmailOnlineRule创建它们。
这样做之后,您需要按照此答案的步骤来扩展规则
形成规则后,您需要通过存储库或模型检查数据库中的记录。
希望此解决方案对您有用。