Home > Net >  How to Use WhereIn Query in Laravel 8
How to Use WhereIn Query in Laravel 8

Time:10-27

Controller

    public function detail(Peserta $peserta)
    {
        // get konfirmasi_id
        $konfirmasi = KonfirmasiPembayaran::where('email',$peserta->email)->select('id')->get();

        $payments   = BankSettlement::whereIn('konfirmasi_id',array($konfirmasi->id))->get();
        // dd($payments);
        $tagihan    = Tagihan::where([['peserta_id', $peserta->id],['type', 3]])->first();
        return view('data.peserta.detail', ['data' => $peserta, 'payments' => $payments,'tagihan' => $tagihan]);
    }

I want to display data from BankSettlement based on konfirmasi_id. Here I try to use WhereIn Query like this, but still error "Property [id] does not exist on this collection instance.".

konfirmasi variable

$konfirmasi has data like the image above.

What is the correct way to display data from BankSettlement based on konfirmasi_id ? Thankyou

CodePudding user response:

Try this changes:

    $konfirmasi = KonfirmasiPembayaran::where('email',$peserta->email)->pluck('id')->toArray();

    $payments   = BankSettlement::whereIn('konfirmasi_id',$konfirmasi)->get();

CodePudding user response:

This is the wrong way to change a collection to array. $payments=BankSettlement::whereIn('konfirmasi_id',array($konfirmasi->id))->get();

You should do this

public function detail(Peserta $peserta)
    {
        // get konfirmasi_id
        $konfirmasi = KonfirmasiPembayaran::where('email',$peserta->email)
                         ->select('id')
                         ->get()
                         ->pluck('id')
                         ->toArray(); //This will return an array of ids

        $payments   = BankSettlement::whereIn('konfirmasi_id',$konfirmasi)->get();
        // dd($payments);
        $tagihan    = Tagihan::where([['peserta_id', $peserta->id],['type', 3]])->first();
        return view('data.peserta.detail', ['data' => $peserta, 'payments' => $payments,'tagihan' => $tagihan]);
    }

Edit: Read Laravel Collections|Pluck

CodePudding user response:

Laravel subquery

$konfirmasi = KonfirmasiPembayaran::where('email',$peserta->email)->select('id');

$payments   = BankSettlement::whereIn('konfirmasi_id', $konfirmasi )->get();
  • Related