首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Laravel中将值从Form和Controller传递到Blade的问题

在Laravel中将值从Form和Controller传递到Blade的问题
EN

Stack Overflow用户
提问于 2018-07-09 17:18:02
回答 0查看 62关注 0票数 1

我将值从地址localhost/ product /1/1发送到我的数据库时出现问题,我希望将产品1附加到客户1,并在下面添加特殊价格我添加代码

路由:

代码语言:javascript
复制
Route::get('/product/{customerID}/{productID}', 'CustomerController@insertCustomerProductForm')->name('add-product');
Route::post('/product/{customerID}/{productID}', 'CustomerController@insertCustomerProductAction');
to Controller

insertCustomerProductForm方法:

代码语言:javascript
复制
public function insertCustomerProductForm($customerID, $productID)

{       
       return view('customer_products.create', [
        'customer' => Customer::find('$customerID'),
        'product' => Product::find('$productID'),        ]);    }

insetCustomerProductAction方法:

代码语言:javascript
复制
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

代码语言:javascript
复制
class CustomerProduct extends Model
{
public function customers()

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

}

public function products()

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

}}

当我尝试在刀片式服务器中为客户的产品设置值时,我只有from表格值(selling_customer_price...等)没有关于产品和客户的内容?我不知道为什么或者这个find方法有问题?因为我必须在这个表格中存储特殊客户的特价。

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

代码语言:javascript
复制
@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_pricepurchase_customer_priceconsumed_customer_price

我有错误:

代码语言:javascript
复制
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))
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51242022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档