12345678910111213141516171819202122232425262728 |
- <?php
- namespace App\Http\Requests;
- use App\Models\User;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Support\Facades\Auth;
- trait RuleHelper
- {
- protected function userCompanyWhere(): \Closure
- {
- return fn (Builder $query) => $query->where('company_id', Auth::user()->company_id);
- }
- protected function usersCompanyRules(): array
- {
- return [
- 'array',
- function ($attribute, $value, $fail) {
- $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
- if ($userCount != count($value)) {
- $fail('The selected user is invalid.');
- }
- }
- ];
- }
- }
|