nested eager loading or hasManyThrough in laravel is not working

70 views Asked by At

i want to get nama from Supplier table using PembelianDetail table through Pembelian table using this code :

$detail = PembelianDetail::with('supplier')->whereBetween('tanggal',[$awal, $akhir])->where('id_produk', $produk->id_produk)->get();

with this model :

class Supplier extends Model
{
    use HasFactory;

    protected $table = 'supplier';
    protected $primaryKey = 'id_supplier';
    protected $guarded = [];

    public function pembelian()
    {
        return $this->hasMany(Pembelian::class, 'id_pembelian', 'id_pembelian');
    }

    public function pembelian_detail(){
        return $this->hasManyThrough(PembelianDetail::class, Pembelian::class );
    }
}


class Pembelian extends Model
{
    use HasFactory;

    protected $table = 'pembelian';
    protected $primaryKey = 'id_pembelian';
    protected $guarded = [];

    public function supplier()
    {
        return $this->belongsTo(Supplier::class, 'id_supplier', 'id_supplier');
    }

    public function pembelian_detail()
    {
        return $this->hasMany(PembelianDetail::class, 'id_pembelian_detail', 'id_pembelian_detail');
    }
}


class PembelianDetail extends Model
{
    use HasFactory;

    protected $table = 'pembelian_detail';
    protected $primaryKey = 'id_pembelian_detail';
    protected $guarded = [];

    
    public function pembelian()
    {
        return $this->belongsTo(PembelianDetail::class, 'id_pembelian_detail', 'id_pembelian_detail');
    }
}

and keep getting this error :

Call to undefined relationship [supplier] on model [App\Models\PembelianDetail].

with the same way i dont understand how that works in this tutorial https://www.youtube.com/watch?v=5s-_SnVl-1g and i just followed the same steps but keep getting that error.

i also tried nested eager loading according laravel documentation by using :

$detail = PembelianDetail::with('pembelian.supplier')->whereBetween('tanggal',[$awal, $akhir])->where('id_produk', $produk->id_produk)->get();

but get the same error and other queries works fine

Am i missing something here?

0

There are 0 answers