如何使用已登录用户 ID 插入数据? Laravel-6

How to insert data with logged in user id? Laravel-6

我想使用产品展示方法中的登录用户 ID 和拍卖 ID 将数据提交给投标 table。

这就是我的 table 之间的关系。

使用 ProductController 好还是我应该创建另一个控制器来出价table?

产品展示blade

 @extends('layouts.app')

 @section('content')

<div class="container">
  <div class="my-3 border">
    <div class="row">
      <div class="col-md-5">
        <img src="/storage/images/{{$product->image}}" alt="" style="width: 100%;">
      </div>
      <div class="col-md-7">
        <h2>{{$product->name}}</h2>
        <p>{{$product->description}}</p>
        <span>Category: {{$product->category->name}}</span><br>
        <span class="text-right">Auction Ends: {{$product->auction->deadline}}</span>
        <div class="price">Initial Price: {{$product->price}}</div>

        {!! Form::open(['action' => 'BiddingsController@create', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
          <div class="form-inline">
            {{Form::number('bidamount', '',['class' => 'form-control mr-1', 'placeholder' => 'Place your bid'])}}
            {{Form::submit('Place Bid', ['class' => 'btn btn-primary'])}}
          </div>
        {!! Form::close() !!}

@endsection

出价控制器

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Bidding;

class BiddingsController extends Controller
{
    public function index()
    {
    $biddings = Bidding::all();

    return view('products.show' ,compact('biddings', $biddings));
}

public function create()
{
   //
}

public function store(Request $request)
{
    //
}

public function show(Bidding $bidding)
{
    //
}

}

在 Laravel 6 应用程序中,您可以使用 auth() 帮助程序获取登录用户。

$authUser = auth()->user();

文档:https://laravel.com/docs/6.x/helpers#method-auth

亲切

您可以随时通过Auth查看您登录的详细信息! 您可以随时随地使用 Auth::user()->id; 例子: 要在控制器中获取登录的用户 ID,请检查!

dd(Auth::user()->id);

如果需要,只需在控制器的开头使用 Illuminate\Support\Facades\Auth。