在Laravel中将值从窗体和控制器传递到刀片服务器时出现问题

在Laravel中将值从窗体和控制器传递到刀片服务器时出现问题,laravel,model,controller,laravel-blade,Laravel,Model,Controller,Laravel Blade,我对将值从地址localhost/product/1/1发送到我的数据库有问题,我想将产品1附加到客户1,并在我添加代码下面添加特殊价格 路线: Route::get('/product/{customerID}/{productID}', 'CustomerController@insertCustomerProductForm')->name('add-product'); Route::post('/product/{customerID}/{productID}', 'Custom

我对将值从地址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'),        ]);    }
InsertCustomerProductAction方法:

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();    }
模型客户产品

class CustomerProduct extends Model
{
public function customers()

{
    return $this->belongsToMany(Customer::class);

}

public function products()

{
    return $this->belongsToMany(Product::class);

}}
当我尝试在客户产品的刀片设置值中使用时,我只有形式值(销售客户价格…等),而没有关于产品和客户的信息?我不知道为什么或者这个查找方法是个问题?因为我必须在这个表单中存储特殊客户的特殊价格

下面我添加了部分刀片代码

@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>

在这两种情况下,您的错误究竟来自哪里
insertCustomerProductForm
应该可以正常工作,只要您在URL中传递正确的参数,如:
localhost/product/20/10


POST请求通常不需要URL本身中的参数。您可以通过在
insertCustomerProductAction
方法中执行
$\u POST['selling\u customer\u price']
来检索POST变量

我刚刚编辑了我的帖子,写了我的文章。我使用了localhost/product/20/10地址,因为我无法获得有关客户和产品的任何信息,也没有写入数据库中,我不知道为什么您可以显示在执行
var\u dump($request->input('selling\u customer\u price')时得到的信息;模具()
在您的
insertCustomerProductAction
方法中?我有/home/vagrant/code/app/Http/Controllers/CustomerController.php:306:var_dump后为null为什么引用变量<代码>客户::查找(“$customerID”)“$customerID”?
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))