feat(usermanagement): optimalkan pencarian dan tambahkan UserFactory

- Ubah pencarian pada `PermissionsController` dan `UsersController`:
  - Gunakan metode `whereRaw` dengan `LOWER` untuk pencocokan case-insensitive pada kolom `name` dan `email`.
- Tambahkan `HasFactory` pada model `User` agar mendukung pembuatan data dummy menggunakan factory.
- Implementasi fungsi `newFactory()` pada model `User` untuk merujuk ke UserFactory.
- Tambahkan file factory baru (`UserFactory`) untuk model `User`:
  - Definisikan state default seperti `name`, `email`, `password`, dan `nik` untuk keperluan pembuatan data dummy.
  - Gunakan password secara default terenkripsi dengan `bcrypt`.
This commit is contained in:
Daeng Deni Mardaeni
2025-05-17 14:12:25 +07:00
parent 33fe30b443
commit 1007515faa
4 changed files with 49 additions and 4 deletions

View File

@@ -268,7 +268,7 @@
if ($request->has('search') && !empty($request->get('search'))) {
$search = $request->get('search');
$query->where(function ($q) use ($search) {
$q->where('name', 'LIKE', "%$search%");
$q->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search) . '%']);
});
}

View File

@@ -79,7 +79,8 @@
$search = $request->get('search');
$query->where(function ($q) use ($search) {
$q
->where('name', 'LIKE', "%$search%")->orWhere('email', 'LIKE', "%$search%");
->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search) . '%'])
->orWhereRaw('LOWER(email) LIKE ?', ['%' . strtolower($search) . '%']);
});
}

View File

@@ -2,6 +2,7 @@
namespace Modules\Usermanagement\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
@@ -24,7 +25,7 @@
*/
class User extends Authenticatable
{
use Notifiable, Userstamps, HasRoles, softDeletes;
use HasFactory, Notifiable, Userstamps, HasRoles, softDeletes;
protected $guard_name = ['web'];
@@ -80,5 +81,14 @@
public function branch(){
return $this->belongsTo(Branch::class);
}
}
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Modules\Usermanagement\Database\Factories\UserFactory::new();
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Modules\Usermanagement\Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Modules\Usermanagement\Models\User;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => bcrypt('password'), // Default password for testing
'remember_token' => Str::random(10),
'nik' => $this->faker->unique()->numerify('##########'),
];
}
}