Reputation:
I want to validate if a user(member) is part of a group via Validation Rules. This is a part of my function...
public function update(Request $request)
{
$request->validate([
'idgroup' => [
'required', 'numeric', new AdminGroupRequest
],
'member' => [
'required', 'numeric', new UserPartOfGroup
],
]);
And this is the Rules Class where I want to check if User is part of group.
public function passes($attribute, $value)
{
$userofgroup = Member::where([['groupid', /*Here I need idgroup*/], ['member', $value/*This is already member*/]])->select('id')->first();
if($userofgroup){
return true;
}else{
return false;
}
}
Any ideas?
Upvotes: 0
Views: 275
Reputation: 35180
You could either use the request() helpers:
public function passes($attribute, $value)
{
return Member::where('groupid', request('idgroup'))
->where('member', $value)
->exists();
}
Or you could just use a closure inside your FormRequest
:
'member' => [
'required', 'numeric', function ($attribute, $value, $fail) {
$exists = Member::where('groupid', $this->input('idgroup'))
->where('member', $value)
->exists();
if (!$exists) {
return $fail('Your error message goes here.');
}
}
],
Upvotes: 1