Postingan lainnya
Relasi many to many laravel property [title] does not exist on this collection instance.
Saya menemukan kendala saat melakukan relasi Many to Many dari tabel : 1. Alternatif
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Alternatif extends Model
{
protected $table = 'alternatif';
protected $fillable = ['id', 'alternatif', 'bahan', 'harga', 'pengatur', 'ukuran', 'garansi'];
public function kriteria(){
return $this->belongsToMany(Kriteria::class)->withPivot(['nilai']);
}
}
2. Kriteria
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Kriteria extends Model
{
protected $table = 'kriteria';
protected $fillable = ['id', 'kriteria', 'type', 'bobot'];
public function alternatif(){
return $this->belongsToMany(Alternatif::class)->withPivot(['nilai']);
}
}
Ketika saya ingin menampilkan datanya menggunakan kode berikut
<table class="table table-striped">
<thead>
<tr>
<th>KODE</th>
<th>NAMA ALTERNATIF</th>
<th>KRITERIA</th>
<th>NILAI</th>
</tr>
</thead>
<tbody>
$alternatif = App\Alternatif::find(1);
@foreach($alternatif->kriteria as $kriteria)
<tr>
<td>{{$kriteria-id}}</td>
<td>{{$kriteria->alternatif}}</td>
<td>{{$kriteria->kriteria}}</td>
<td>{{$kriteria->nilai}}</td>
</tr>
@endforeach
</tbody>
</table>
Muncul Error
Property [kriteria] does not exist on this collection instance. (View: D:\LARAVEL\moora\resources\views\penilaian.blade.php)
Apakah ada langkah-langkah yang terlewatkan dalam merelasikan data?
Tanggapan
Sudah adakah solusinya?
2 Jawaban:
screenshot table di database sama error nya ada kah?
ada mas database <img src='https://scontent-sin6-1.xx.fbcdn.net/v/t1.0-9/74958637_2507867185976688_1096380197185257472_n.jpg?_nc_cat=101&_nc_oc=AQlmjgAkbcQ_lplw9CmERiUonB8_RmqoHx-RRV7Nhy9wbLP4XpZLHLZCxW0-M_UrxaM&_nc_ht=scontent-sin6-1.xx&oh=f481be62f2d1fcbac0061fe61240c1ef&oe=5E46B8A8'>
error Property [kriteria] does not exist on this collection instance. (View: D:\LARAVEL\moora\resources\views\penilaian.blade.php)