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:
@@ -268,7 +268,7 @@
|
|||||||
if ($request->has('search') && !empty($request->get('search'))) {
|
if ($request->has('search') && !empty($request->get('search'))) {
|
||||||
$search = $request->get('search');
|
$search = $request->get('search');
|
||||||
$query->where(function ($q) use ($search) {
|
$query->where(function ($q) use ($search) {
|
||||||
$q->where('name', 'LIKE', "%$search%");
|
$q->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search) . '%']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,7 +79,8 @@
|
|||||||
$search = $request->get('search');
|
$search = $request->get('search');
|
||||||
$query->where(function ($q) use ($search) {
|
$query->where(function ($q) use ($search) {
|
||||||
$q
|
$q
|
||||||
->where('name', 'LIKE', "%$search%")->orWhere('email', 'LIKE', "%$search%");
|
->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search) . '%'])
|
||||||
|
->orWhereRaw('LOWER(email) LIKE ?', ['%' . strtolower($search) . '%']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Modules\Usermanagement\Models;
|
namespace Modules\Usermanagement\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
@@ -24,7 +25,7 @@
|
|||||||
*/
|
*/
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
use Notifiable, Userstamps, HasRoles, softDeletes;
|
use HasFactory, Notifiable, Userstamps, HasRoles, softDeletes;
|
||||||
|
|
||||||
protected $guard_name = ['web'];
|
protected $guard_name = ['web'];
|
||||||
|
|
||||||
@@ -80,5 +81,14 @@
|
|||||||
public function branch(){
|
public function branch(){
|
||||||
return $this->belongsTo(Branch::class);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
34
database/factories/UserFactory.php
Normal file
34
database/factories/UserFactory.php
Normal 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('##########'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user