I finish to make relation in my model, but i dont know to passing data relation to view, hope you can help me.
Model Siswa
public function Absen()
{
return $this->hasMany(Absen::class);
}
Model Absen
public function Siswa()
{
$this->belongsTo(Siswa::class);
}
Absen Table
Siswa Table
AbsenController@index
public function index()
{
$absen = Absen::where('level', '=', 'Siswa')->get();
return view('absen.index')->with('data', $absen);
}
index.blade.php
@foreach($data as $index => $value)
<tr>
<td>{{ $index+1 }}</td>
<td>{{ $value->nama }}</td>
<td>{{ $value->keterangan }}</td>
<td>
{!! Form::open(['route' => ['siswa.destroy', $value->id],
'method' => 'DELETE']) !!}
{{ Form::submit('Hapus', ['class' => 'btn btn-danger']) }}
<a href="{{ route('siswa.edit', $value->id) }}" class="btn
btn-warning">Edit</a>
{!! Form::close() !!}
</td>
</tr>
@endforeach
The problem is the $value->nama not showing in my view, but i done to create relation in table siswa and table absen.Thank
nama is not an attribute of Absen class, yet your value is an Absen class. Though you have defined the relationship between Absen and Siswa, attributes of Siswa are still not directly inherited to Absens. You can first get the Siswa of the Absen and then get the attributes of the Siswa.
So what you have to do is:
Call $value->Siswa->nama
instead of $value->nama
, and you should have your nama displayed.