0

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?

  • Welcome to SO ... you have the `pembelian` relationship on `PembelianDetail` referencing itself, `PembelianDetail`, instead of the `Pembelian` model: `$this->belongsTo(PembelianDetail::class, ...)` ... the `PembelianDetail` model does not have a relationship named `supplier` – lagbox Oct 13 '22 at 04:42
  • oh my god, thank you for noticing that. the problem is as u said pembelian in pembeliandetail is referencing itself im so stupid but thank you anyway – NAUFAL NAFIDIIN Oct 13 '22 at 05:27

0 Answers0