I tried to create view page with get name sekolah based on the id, but the result is "Use of undefined constatnt..."
Here's the controller codes from GuruController.php for viewing guru data:
public function show(Guru_Model $guru)
{
$ids = [];
return view(
'page/guru/view',
[
'guru' => Guru_Model::findorfail($guru),
]
);
}
Here's the model code for guru table (Guru_Model.php):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Guru_Model extends Model
{
use SoftDeletes;
protected $table = "tb_guru";
protected $primaryKey = 'guru_id';
protected $fillable = [
'sekolah_id',
'guru_nama',
'guru_tgl_lahir',
'guru_jekel',
'guru_email',
'guru_notelp',
'guru_alamat',
];
public function sekolah()
{
return $this->belongsTo(Sekolah_Model::class, sekolah_id, sekolah_id);
}
public function nilai()
{
return $this->hasOne(Kriteria_Penilaian_Model::class);
}
}
Here's the sekolah model code (Sekolah_Model.php):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Sekolah_Model extends Model
{
use SoftDeletes;
protected $table = "tb_sekolah";
protected $primaryKey = 'sekolah_id';
protected $fillable = [
'sekolah_npsn',
'sekolah_nama',
'sekolah_alamat',
];
public function sekolah()
{
return $this->hasMany(Guru_Model::class);
}
}
Here's the view code (view.blade.php) for viewing guru data:
<div class="card-body">
<h4 class="card-title text-center">Guru</h4>
<hr>
@foreach($guru as $guru)
<div class="form-group">
<div class="form-label">Nama</div>
<div></div>
</div>
<div class="form-group">
<label>Sekolah</label>
<div></div>
</div>
<div class="form-group">
<label>Tanggal Lahir</label>
<div></div>
</div>
<div class="form-group">
<label>Jenis Kelamin</label>
<div></div>
</div>
<div class="form-group">
<label>No Telepon</label>
<div></div>
</div>
<div class="form-group">
<label>Email</label>
<div></div>
</div>
<div class="form-group">
<label>Alamat</label>
<div></div>
</div>
@endforeach
<div class="border-top">
<div class="card-body">
<button class="btn btn-warning" type="button" onclick="window.history.back()">Back</button>
</div>
</div>
When I replace $guru->sekolah_id to $guru->sekolah->sekolah_nama in view.blade.php, it resulted error. How to fix this? Is something wrong with the model?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire