Penyesuaian Model
Contents
Model yang sudah kita miliki perlu disesuaikan karena beberapa hal:
- Perlu adanya penyesuaian nama tabel (property
$table
) yang tidak sesuai dengan konvensi Laravel (tidak plural) - Perlu ada penyesuaian property
$fillable
dan$dates
- Perlu pendefinisian relationship antar tabel, agar mempermudah saat melakukan query data
Penyesuaian pada model Pengaduan:
class Pengaduan extends Model { use HasFactory; protected $table = 'pengaduan'; protected $fillable = ['tgl_pengaduan', 'user_id', 'isi_laporan', 'foto', 'status' ]; protected $dates = ['tgl_pengaduan']; public function user() { return $this->belongsTo(User::class); } public function tanggapan() { return $this->hasMany(Tanggapan::class); } }
Penyesuaian pada model Tanggapan:
class Tanggapan extends Model { use HasFactory; protected $table = 'tanggapan'; protected $fillable = ['pengaduan_id', 'tgl_tanggapan', 'tanggapan', 'petugas_id' ]; protected $dates = ['tgl_tanggapan']; public function petugas() { return $this->belongsTo(User::class,'petugas_id'); } }
Penyesuaian pada model User, mencakup apa yang sudah dikerjakan sebelumnya, serta penambahan relationship:
class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', 'nik', 'telp', 'level' ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = [ 'email_verified_at' => 'datetime', ]; public function pengaduan() { return $this->hasMany(Pengaduan::class); } public function tanggapan() { return $this->hasMany(Tanggapan::class); } }