Laravel Auth::user()->id returns livewire 表单上为空

Laravel Auth::user()->id returns null on livewire form

我尝试将 Auth::user()->id 作为我的表单值。它可以显示在标签上,但它 return 值 null.

查看:

<option value="{{ Auth::user()->id }}" data-keterangan="">{{ Auth::user()->id }}</option>

Http/Livewire :

        $this->validate([
            'customer_code'=>'required|string',
            'customer_name'=>'required|string',
            'phone_number'=>'required|string'
        ]);

        StokItem::updateOrCreate([
            'customer_code'=>$this->customer_code,
            'customer_name'=>$this->customer_name,
            'phone_number'=>$this->phone_number
        ]);

    

型号:

    protected $fillable = ['id','customer_code','customer_name','phone_number','produk_code','produk_name','produk_uom','qty','kode_kondisi_barang','jenis_kondisi_barang','remark','url_foto','status'];

确保您的用户已通过身份验证

@auth
  <option value="{{ Auth::user()->id }}" data-keterangan="">{{ Auth::user()->id }}</option>
@endauth

您不能执行 auth()->name(),您需要通过执行 auth()->user() 来调用经过身份验证的用户,然后获取数据库中的列,例如 auth()->user()->name

(https://laravel.com/docs/8.x/helpers#method-auth)

在你的Livewire Component中,你应该这样做

public $id = '';
public $name = '';     
public $no_hp = '';

public function mount()
{
   $this->id = auth()->id();         
   $this->name = auth()->user()->name;         
   $this->no_hp = auth()->user()->no_hp;
}