首页 > 解决方案 > 如何从 eloquent 选择变量

问题描述

我有这个从表 petugas 中选择 id 的代码,并且有声明 where from auth

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

我想把那个id放到这段代码中

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
        ]);

如何获取这些变量的输入?

标签: laravelvariableseloquentlaravel-authentication

解决方案


使用 first() 方法:

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

然后

echo $petugas->ID_PETUGAS;

推荐阅读