laravel 8 自动添加行和写入用户标识?

laravel 8 add row and write userid automatically?

我遵循了这个 quida,一切正常,但我需要 userid 字段自动写入登录用户的 id,我该怎么做?我怎样才能将它添加到此代码中?

控制器:

<?php

命名空间App\Http\Controllers;

使用Illuminate\Http\Request;

使用App\ProductStock;

class ProductAddMoreController 扩展控制器

{

/**

 * Display a listing of the resource.

 *

 * @return \Illuminate\Http\Response

 */

public function addMore()

{

    return view("addMore");

}



/**

 * Display a listing of the resource.

 *

 * @return \Illuminate\Http\Response

 */

public function addMorePost(Request $request)

{

    $request->validate([

        'addmore.*.name' => 'required',

        'addmore.*.qty' => 'required',

        'addmore.*.price' => 'required',

    ]);



    foreach ($request->addmore as $key => $value) {

        ProductStock::create($value);

    }



    return back()->with('success', 'Record Created Successfully.');

}

}

查看:

<!DOCTYPE html>

<html>

<head>

    <title>Add/remove multiple input fields dynamically with Jquery Laravel 5.8</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

</head>

<body>

   

<div class="container">

    <h2 align="center">Add/remove multiple input fields dynamically with Jquery Laravel 5.8</h2> 

   

    <form action="{{ route('addmorePost') }}" method="POST">

        @csrf

   

        @if ($errors->any())

            <div class="alert alert-danger">

                <ul>

                    @foreach ($errors->all() as $error)

                        <li>{{ $error }}</li>

                    @endforeach

                </ul>

            </div>

        @endif

   

        @if (Session::has('success'))

            <div class="alert alert-success text-center">

                <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>

                <p>{{ Session::get('success') }}</p>

            </div>

        @endif

   

        <table class="table table-bordered" id="dynamicTable">  

            <tr>

                <th>Name</th>

                <th>Qty</th>

                <th>Price</th>

                <th>Action</th>

            </tr>

            <tr>  

                <td><input type="text" name="addmore[0][name]" placeholder="Enter your Name" class="form-control" /></td>  

                <td><input type="text" name="addmore[0][qty]" placeholder="Enter your Qty" class="form-control" /></td>  

                <td><input type="text" name="addmore[0][price]" placeholder="Enter your Price" class="form-control" /></td>  

                <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>  

            </tr>  

        </table> 

    

        <button type="submit" class="btn btn-success">Save</button>

    </form>

</div>

   

<script type="text/javascript">

   

    var i = 0;

       

    $("#add").click(function(){

   

        ++i;

   

        $("#dynamicTable").append('<tr><td><input type="text" name="addmore['+i+'][name]" placeholder="Enter your Name" class="form-control" /></td><td><input type="text" name="addmore['+i+'][qty]" placeholder="Enter your Qty" class="form-control" /></td><td><input type="text" name="addmore['+i+'][price]" placeholder="Enter your Price" class="form-control" /></td><td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>');

    });

   

    $(document).on('click', '.remove-tr', function(){  

         $(this).parents('tr').remove();

    });  

   

</script>

  

</body>

</html>

有谁知道如何帮助我在数据库中自动插入用户 ID,我不需要在 table 中看到它,但我需要在数据库中使用这些相关字段注册它

Laravel 包括 built-in 身份验证和会话服务,通常通过 Auth and Session 门面访问。 userid可以这样获取Auth::user()->id 在 ProductStock 模型的代码 ProductAddMoreController 中使用此标识符。

有不止一种方法可以做到这一点,您可以使用模型观察者来完成,也可以通过手动将 user_id 值添加到 create 方法来完成。

首先,您需要在 products_stocks 数据库 table 中包含 user_id 列,您可以使用类似以下内容将其添加到迁移中的 up() 方法中:

public function up()
{
    Schema::table('products_stocks', function (Blueprint $table) {
        $table->unsignedBigInteger('user_id')->nullable();

        $table->foreign('user_id')
            ->references('id')->on('users')
            ->onDelete('cascade')->onUpdate('cascade');
    });
}

然后在你的控制器中添加 user_idcreate() 方法:

// ...

foreach ($request->addmore as $key => $value) {
    ProductStock::create(array_merge($value, ['user_id' => auth()->user()->id]));
}

// ...