Search code examples
laravelvariableseloquentlaravel-authentication

how to make variable from eloquent select


i have this code for select id from table petugas and have statement where from auth

$petugas = petugas::where('ID_PETUGAS','=',auth()->user()->id);

and i want put that id to this code

Peminjaman::create([
            'ID_ANGGOTA' => $request->ID_ANGGOTA,
            'ID_BUKU' => $request->ID_BUKU,
            'ID_PETUGAS' => $petugas->ID_PETUGAS, --> to this
            'TANGGAL_PINJAM' => $request->TANGGAL_PINJAM,
            'TANGGAL_KEMBALI' => $request->TANGGAL_KEMBALI
        ]);

how to take the input of these variables ?


Solution

  • Use the first() method:

    $petugas = petugas::where('ID_PETUGAS','=',auth()->user()->id)->first();
    

    Then

    echo $petugas->ID_PETUGAS;