I want to pass a variable from a function passes to function message. I create $groupname in the else sector and try to use it in the message function query. What is in my case the best way to do this?
class ExistingGroupMembers implements Rule
{
public function __construct()
{
//
}
public function passes($attribute, $value)
{
$existingMembers = Member::where([['idgroup', $value],['member', '<>', Auth::id()]])->select('id')->first('id');
if(!empty($existingMembers)){
return true;
}else{
$groupname = Group::where('id', $value)->select('name')->first();
return false;
}
}
public function message()
{
return 'In der Gruppe ' .$groupname. ' existieren keine User!';
}
}
add your variables to $this
scope. then you can access it.
example code
public function passes()
{
$this->group_name = Group::where('id', $value)->select('name')->first();
}
public function message()
{
return 'In der Gruppe ' . $this->group_name . ' existieren keine User!';
}
You can try to create a private property of class then then set value in passes
function then you can access that property in message
function
class ExistingGroupMembers implements Rule
{
private $groupname;
public function __construct()
{
//
}
public function passes($attribute, $value)
{
$existingMembers = Member::where([['idgroup', $value],['member', '<>', Auth::id()]])->select('id')->first('id');
if(!empty($existingMembers)){
return true;
}else{
$this->groupname = Group::where('id', $value)->select('name')->first();
return false;
}
}
public function message()
{
return 'In der Gruppe ' .$this->groupname. ' existieren keine User!';
}
}