Reputation: 19
I Have Problem with get data
This is my code Controller
$class = DetailMentor::with('Kelas')->where('id', Auth::user()->id)->get();
Model:
protected $primaryKey = 'id_mentor';
protected $fillable = [
'id',
'id_kelas',
];
public $timestamps = true;
public function Kelas()
{
return $this->hasMany(Kelas::class, 'id_kelas','id_kelas');
}
public function User()
{
return $this->belongsTo(User::class, 'id','id');
View index
<tbody>
@forelse ($class->Kelas as $item)
<tr role="row" class="odd">
<th scope="row" class="small" class="sorting_1">{{ $loop->iteration}}.</th>
<td>{{ $item->nama_kelas }}</td>
<td>{{ $item->Jeniskelas->jenis_kelas }}</td>
<td>{{ $item->level->nama_level }}</td>
<td class="text-center">
@if ($item->status_video == 'Telah Dibuat')
<span class="badge badge-success ">Telah Dibuat</span>
@else
<span class="badge badge-danger">Belum Dibuat</span>
@endif
</td>
</tr>
@empty
@endforelse
</tbody>
Error: Property [Kelas] does not exist on this collection instance. (View: C:\laragon\www\Bootcamp\resources\views\mentor\listkelas\index.blade.php)
I want to group data kelas by id. If i use first
it only takes one data, If i use get it say error. Can someone Helpme, Please! I'm new to this
[Picture: Array Return $class][1] [1]: https://i.sstatic.net/0fjIj.png
Upvotes: 1
Views: 1014
Reputation: 468
the first ensure if the data get correctly test it on postman or on browser you can use method dd()
so if the data come correct
the problem in view index.blade.php
I think it works on first
method because you get one object (one record)
and it doesn't work for get
method because it gets array of object so the way of display this array it is different from the one object
ensure the way of display it and use foreach in index.blade.php
hopefully that is help you
update:
@foreach ($class as $item)
<tr role="row" class="odd">
<th scope="row" class="small" class="sorting_1">{{ $loop->iteration}}.</th>
<td>{{ $item->kelas[0]->nama_kelas }}</td>
<td>{{ $item->kelas[0]->Jeniskelas->jenis_kelas }}</td>
<td class="text-center">
@if ($item->kelas[0]->status_video == 'Telah Dibuat')
<span class="badge badge-success ">Telah Dibuat</span>
@else
<span class="badge badge-danger">Belum Dibuat</span>
@endif
</td>
</tr>
@empty
@endforeach
you can access like this $item->kelas[0]->nama_kelas
Upvotes: 2