for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Spatie\ValidationRules\Rules;
use Illuminate\Contracts\Validation\Rule;
class ModelsExist implements Rule
{
/** @var string */
protected $modelClassName;
protected $modelAttribute;
protected $attribute;
/** @var array */
protected $modelIds;
public function __construct(string $modelClassName, string $attribute = 'id')
$this->modelClassName = $modelClassName;
$this->modelAttribute = $attribute;
}
public function passes($attribute, $value): bool
$this->attribute = $attribute;
$value = array_filter($value);
$this->modelIds = array_unique($value);
$modelCount = $this->modelClassName::whereIn($this->modelAttribute, $this->modelIds)->count();
whereIn
$this->modelClassName
string
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.
return count($this->modelIds) === $modelCount;
public function message(): string
$modelIds = implode(', ', $this->modelIds);
$classBasename = class_basename($this->modelClassName);
return __('validationRules::messages.model_ids', [
'attribute' => $this->attribute,
'model' => $classBasename,
'modelAttribute' => $this->modelAttribute,
'modelIds' => $modelIds,
]);
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.