我从地址localhost / product / 1/1向我的数据库发送值时遇到问题,我想将产品1附加到客户1并在我添加代码的下方添加特殊价格
路线:
Route::get('/product/{customerID}/{productID}', 'CustomerController@insertCustomerProductForm')->name('add-product');
Route::post('/product/{customerID}/{productID}', 'CustomerController@insertCustomerProductAction');
to Controller
insertCustomerProductForm方法:
public function insertCustomerProductForm($customerID, $productID)
{
return view('customer_products.create', [
'customer' => Customer::find('$customerID'),
'product' => Product::find('$productID'), ]); }
insetCustomerProductAction方法:
public function insertCustomerProductAction (Request $request, $customerID, $productID) {
$newCustomerProduct = new CustomerProduct;
$newCustomerProduct->product_id = $productID;
$newCustomerProduct->customer_id = $customerID;
$newCustomerProduct->selling_customer_price = $request->input('selling_customer_price');
$newCustomerProduct->purchase_customer_price = $request->input('purchase_customer_price');
$newCustomerProduct->consumed_customer_price = $request->input('consumed_customer_price');
$newCustomerProduct->save(); }
模拟CustomerProduct
class CustomerProduct extends Model
{
public function customers()
{
return $this->belongsToMany(Customer::class);
}
public function products()
{
return $this->belongsToMany(Product::class);
}}
,当我尝试使用客户产品的刀片设置值时,我仅从表单值(selling_customer_price ...等)中没有任何关于产品和客户的信息?我不知道为什么还是这种查找方法有问题?因为我必须以这种形式存储特殊客户的特殊价格。
在我下面添加刀片代码的一部分
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dodaj Produkt') }} </div>
<div class="card-body">
<form method="post">
@csrf
<div class="form-group row">
{{ $customer }}
<label for="selling_customer_price " class="col-md-4 col-form-label text-md-right">{{ __('Cena Sprzedaży') }}</label>
<div class="col-md-6">
<input id="selling_customer_price " type="text" class="form-control{{ $errors->has('selling_customer_price ') ? ' is-invalid' : '' }}" name="selling_customer_price " value="" required autofocus>
@if ($errors->has('selling_customer_price '))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('selling_customer_price ') }}</strong>
</span>
@endif
</div>
我的错误看起来好像看不到表单中的变量:
selling_customer_price
,purchase_customer_price
,consumed_customer_price
我有错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'selling_customer_price' cannot be null (SQL: insert into `customer_products` (`product_id`, `customer_id`, `selling_customer_price`, `purchase_customer_price`, `consumed_customer_price`, `updated_at`, `created_at`) values (1, 1, , , , 2018-07-09 12:39:03, 2018-07-09 12:39:03))
答案 0 :(得分:1)
两种情况下您的错误究竟出自哪里?只要您在网址中传递正确的参数,insertCustomerProductForm
就能正常工作:localhost/product/20/10
POST请求通常不需要URL本身中的参数。您可以通过在$_POST['selling_customer_price']
方法中执行insertCustomerProductAction
来检索POST变量。