laravel 我将得到“SQLSTATE[23000]:完整性约束冲突”

omhiaaxx  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(165)

当我尝试获取一个产品并对其执行命令时,我会得到“Illuminate\Database\QueryException SQLSTATE[23000]:完整性约束冲突:1048数据行'name'不可以是空值(SQL:insert into commandenamefamilynamequantitymobilevilleadresseid_productuser_idupdated_atcreated_at)values(?,?,?,?,?,?,?,?,2022 - 11 - 21 21:30:27,2022 - 11 - 21 21:30:27))”我在这里尝试命令一个产品,其中每个产品都有不同的用户。我在产品表(user_id)中使用外键,并且每个命令都有一个用户来检查它。
这是我在控制器中的函数:

public function getProduct($id, Request $request)
{

    $product = Product::find($id);
    $commande = new AppCommande;
    $commande->name = $request->input('name');
    $commande->familyname = $request->input('familyname');
    $commande->quantity = $request->input('quantity');
    $commande->mobile = $request->input('mobile');
    $commande->ville = $request->input('ville');
    $commande->adresse = $request->input('adresse');
    $commande->id_product = $request->input('id_product');
    $commande->user_id = $request->input('id_user');
    $commande->save();
    return view('product', ['product' => $product], ['commande' => $commande]);

}

这是我的路线:

Route::get('/product/{id}', \[ 'uses' =\> 'CommandeUserController@getProduct', 'as' =\> 'product.single' \]);

这是一个视图:

@extends('layouts.app')
@section('content')

    <div class="col-sm-6 col-md-4">
        <div class="thumbnail">
            <img src="{{ asset('uploads/product/'.$product->image) }}" width="90px" alt="image">
            <div class="caption">
                <h3> {{$product->name}} </h3>
                <p class="discription"> {{$product->description}} </p>
                <div class="clearfix">
                    <div class="pull-left price"/>$ {{$product->price}}</div>
              {{-- <a href= {{ route('commander', ['id' => $product->id ]) }} class="btn btn-danger pull-right" role="button">Commander ce produit</a> --}}
            </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header">
            Create Commande
        </div>
    
        <div class="card-body">
            <form action="{{ route("admin.commandes.store") }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
                    <label for="name">Name</label>
                    <input type="text" id="name" name="name" class="form-control" value="{{ old('name', isset($commande) ? $commande->name : '') }}">
                    @if($errors->has('name'))
                        <em class="invalid-feedback">
                            {{ $errors->first('name') }}
                        </em>
                    @endif
                    <p class="helper-block">
                        {{ trans('global.product.fields.name_helper') }}
                    </p>
                </div>
                <div class="form-group {{ $errors->has('familyname') ? 'has-error' : '' }}">
                    <label for="name">Family Name</label>
                    <input type="text" id="familyname" name="familyname" class="form-control" value="{{ old('familyname', isset($commande) ? $commande->familyname : '') }}">
                    @if($errors->has('name'))
                        <em class="invalid-feedback">
                            {{ $errors->first('name') }}
                        </em>
                    @endif
                    <p class="helper-block">
                        {{ trans('global.product.fields.name_helper') }}
                    </p>
                </div>
                <div class="form-group {{ $errors->has('mobile') ? 'has-error' : '' }}">
                    <label for="quantity">Mobile</label>
                    <input type="number" id="mobile" name="mobile" class="form-control" value="{{ old('mobile', isset($commande) ? $commande->mobile : '') }}" step="1">
                    @if($errors->has('mobile'))
                        <em class="invalid-feedback">
                            {{ $errors->first('mobile') }}
                        </em>
                    @endif
                    <p class="helper-block">
                        {{ trans('global.product.fields.price_helper') }}
                    </p>
                </div>
                <div class="form-group {{ $errors->has('quantity') ? 'has-error' : '' }}">
                    <label for="quantity">Quantity</label>
                    <input type="number" id="quantity" name="quantity" class="form-control" value="{{ old('quantity', isset($commande) ? $commande->quantity : '') }}" step="1">
                    @if($errors->has('price'))
                        <em class="invalid-feedback">
                            {{ $errors->first('price') }}
                        </em>
                    @endif
                    <p class="helper-block">
                        {{ trans('global.product.fields.price_helper') }}
                    </p>
                </div>
                <div class="form-group {{ $errors->has('ville') ? 'has-error' : '' }}">
                    <label for="ville">City</label>
                    <input type="text" id="ville" name="ville" class="form-control" value="{{ old('ville', isset($commande) ? $commande->familyname : '') }}">
                    @if($errors->has('ville'))
                        <em class="invalid-feedback">
                            {{ $errors->first('ville') }}
                        </em>
                    @endif
                    <p class="helper-block">
                        {{ trans('global.product.fields.name_helper') }}
                    </p>
                </div>
                <div class="form-group {{ $errors->has('adresse') ? 'has-error' : '' }}">
                    <label for="adress">Adresse</label>
                    <input type="text" id="adresse" name="adresse" class="form-control" value="{{ old('adresse', isset($commande) ? $commande->adresse : '') }}">
                    @if($errors->has('adresse'))
                        <em class="invalid-feedback">
                            {{ $errors->first('adresse') }}
                        </em>
                    @endif
                    <p class="helper-block">
                        {{ trans('global.product.fields.name_helper') }}
                    </p>
                </div>
                <input type="hidden" name="id_product" value=" {{ $product->id }}" />
                <input type="hidden" name="user_id" value=" {{ $product->user_id }}" />
    
                    <input class="btn btn-danger" type="submit" value="{{ trans('global.save') }}">
                </div>
            </form>
        </div>
    </div>
     @endsection
irlmq6kh

irlmq6kh1#

因为我觉得奇怪,你的错误消息没有从你的请求中得到任何值,所以试着把你的$commande变量添加到你最后一个模型初始化的标准括号'()'中。

$commande = new AppComande();

相关问题