51 lines
930 B
PHP
51 lines
930 B
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use App\Models\Category;
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
class CategoryHasCommands implements Rule
|
|
{
|
|
/**
|
|
* Create a new rule instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Determine if the validation rule passes.
|
|
*
|
|
* @param string $attribute
|
|
* @param mixed $value
|
|
* @return bool
|
|
*/
|
|
public function passes($attribute, $value)
|
|
{
|
|
if ($value == null) {
|
|
return false;
|
|
}
|
|
|
|
$result = Category::with('command')->find($value);
|
|
if ($result === null) {
|
|
return false;
|
|
}
|
|
|
|
return $result->command->count() > 0;
|
|
}
|
|
|
|
/**
|
|
* Get the validation error message.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function message()
|
|
{
|
|
return 'A category must have commands associated with it.';
|
|
}
|
|
}
|