Files
Global-Jain/app/Rules/ViharDoesNotStoreForSameDateTime.php
2025-11-05 10:37:10 +05:30

65 lines
1.7 KiB
PHP

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use App\Models\Vihar;
use Carbon\Carbon;
class ViharDoesNotStoreForSameDateTime implements Rule
{
public $sant_id;
public $start_date;
public $start_time;
public $vihar_id;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($data, $vihar_id = null)
{
$this->sant_id = $data['sant_id'];
$this->start_date = $data['start_date'];
$this->start_time = $data['start_time'];
$this->vihar_id = $vihar_id;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$santID = $this->sant_id;
$numberOfPosts = Vihar::where('sant_id', $this->sant_id)
// ->orWhere( function ($query) use ($santID) {
// return $query->whereHas('viharThana', function ($subQuery) use ($santID) {
// $subQuery->where('vihar_thanas.sant_id', $santID);
// })->orWhere('vihars.sant_id', $santID);
// })
->where('id', '<>', $this->vihar_id)
->where('start_date', $this->start_date)
->where('start_time', $this->start_time)->whereDate('created_at', Carbon::today())->count();
if ($numberOfPosts > 0) {
return false;
} else {
return true;
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Vihaar already exists for this date and time.';
}
}