我按照this quida操作,一切正常,但我需要自动使用登录用户的id写入userid字段,我该怎么做?我怎样才能将其添加到此代码中?
控制器:
<?php
命名空间 App\Http\Controllers;
使用 Illuminate\Http\Request;
使用App\ProductStock;
类 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,我不需要在表中看到它,但我需要它与这些关联字段一起在数据库中注册
Laravel 包含内置的身份验证和会话服务,通常通过 Auth 和 Session 外观访问。 Userid 可以这样获取
Auth::user()->id
在 ProductAddMoreController 代码中为 ProductStock 模型使用此标识符。
有不止一种方法可以做到这一点,您可以使用模型观察者来完成,也可以通过手动将
user_id
值添加到 create
方法来完成。
首先,您需要在
user_id
数据库表中拥有 products_stocks
列,您可以使用以下方法将其添加到迁移中的 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_id
添加到 create()
方法:
// ...
foreach ($request->addmore as $key => $value) {
ProductStock::create(array_merge($value, ['user_id' => auth()->user()->id]));
}
// ...