File: /home/armgroup/libs/app/Http/Livewire/Admin/User/Edit.php
<?php
namespace App\Http\Livewire\Admin\User;
use Carbon\Carbon;
use App\Models\User;
use App\Rules\Mobile;
use Livewire\Component;
use App\Models\Organization;
use App\Http\Livewire\General;
use App\Models\SellerInformation;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use App\Http\Livewire\ConvertEmptyStringsToNull;
class Edit extends Component
{
use ConvertEmptyStringsToNull, General;
public User $user;
public $roles;
public $role_id;
public $password;
public $email_status;
public $organizations;
public $company;
public $career_field;
public $number_whatsapp;
public $number_rubika;
public $postal_code;
public $registration_number;
public function mount()
{
$this->roles = Role::query()->get(['id', 'name']);
$this->user->is_seller = $this->user->is_seller ? 1 : 0;
$this->role_id = optional($this->user->roles()->first())->id;
$this->email_status = $this->user->email_verified_at ? 1 : 0;
$this->organizations = Organization::query()->get(['id', 'name']);
if ($this->user->is_seller) {
$this->user->load('sellerInformation');
$this->company = $this->user->sellerInformation->company;
// $this->registration_number = $this->user->sellerInformation->registration_number;
// $this->economic_code = $this->user->sellerInformation->economic_code;
// $this->phone = $this->user->sellerInformation->phone;
// $this->name_of_ceo = $this->user->sellerInformation->name_of_ceo;
// $this->organization_level = $this->user->sellerInformation->organization_level;
$this->postal_code = $this->user->sellerInformation->postal_code;
$this->career_field = $this->user->sellerInformation->career_field;
$this->number_whatsapp = $this->user->sellerInformation->number_whatsapp;
$this->number_rubika = $this->user->sellerInformation->number_rubika;
}
}
protected function rules()
{
return [
'user.name' => 'required|string|max:250',
'user.mobile' => ['required', 'unique:users,mobile,'.$this->user->id, new Mobile],
'user.email' => 'nullable|email|max:250|unique:users,email,'.$this->user->id,
'password' => 'nullable|string|min:8|max:250',
'user.active' => 'required|in:0,1',
'email_status' => 'required|in:0,1',
'role_id' => 'nullable|exists:roles,id',
'user.organization_id' => 'nullable|exists:organizations,id',
'user.is_seller' => 'required|boolean',
'company' => ['nullable', 'required_if:user.is_seller,1', 'max:200'],
// 'registration_number' => ['nullable', 'required_if:user.is_seller,1', 'digits_between:10,12'],
// 'economic_code' => ['nullable', 'required_if:user.is_seller,1', 'digits_between:11,12'],
// 'phone' => ['nullable', 'required_if:user.is_seller,1', 'max:20'],
// 'name_of_ceo' => ['nullable', 'required_if:user.is_seller,1', 'max:100'],
// 'organization_level' => ['nullable', 'required_if:user.is_seller,1', 'max:200'],
// 'postal_code' => ['nullable', 'required_if:user.is_seller,1', 'digits:10'],
'postal_code' => ['nullable', 'digits:10'],
'career_field' => ['nullable', 'required_if:user.is_seller,1', 'string'],
'number_whatsapp' => ['nullable', 'required_if:user.is_seller,1', 'max:20'],
'number_rubika' => ['nullable', 'required_if:user.is_seller,1', 'max:20'],
];
}
public function render()
{
return view('livewire.admin.user.edit');
}
public function update()
{
$this->validate();
$data = [
'name' => $this->user->name,
'email' => $this->user->email,
'active' => $this->user->active,
'organization_id' => $this->user->organization_id,
'mobile' => to_latin_numbers($this->user->mobile),
'email_verified_at' => ($this->email_status && $this->user->email) ? ($this->user->email_verified_at ?: Carbon::now()) : null,
];
if ($this->password) {
$data['password'] = Hash::make($this->password);
}
DB::transaction(function () use ($data) {
$this->user->update($data);
if ($this->role_id) {
$this->user->syncRoles([$this->role_id]);
} else {
$this->user->roles()->detach();
}
if ($this->user->is_seller) {
SellerInformation::updateOrCreate(
['user_id' => $this->user->id],
[
'company' => $this->company,
// 'registration_number' => $this->registration_number,
// 'economic_code' => $this->economic_code,
// 'phone' => $this->phone,
// 'name_of_ceo' => $this->name_of_ceo,
// 'organization_level' => $this->organization_level,
'postal_code' => $this->postal_code,
'career_field' => $this->career_field,
'number_whatsapp' => $this->number_whatsapp,
'number_rubika' => $this->number_rubika,
]
);
} else {
$this->user->sellerInformation()->delete();
}
saveActivity("کاربر {$this->user->name} ویرایش شد", $this->user);
});
$this->doneMessage("کاربر {$this->user->name} با موفقیت ویرایش شد");
return redirect()->route('admin.users.index');
}
}