When I try to get a product and command it I get "IlluminateDatabaseQueryException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘name’ cannot be null (SQL: insert into commande
(name
, familyname
, quantity
, mobile
, ville
, adresse
, id_product
, user_id
, updated_at
, created_at
) values (?, ?, ?, ?, ?, ?, ?, ?, 2022-11-21 21:30:27, 2022-11-21 21:30:27))"
I am trying here to command a product where every product has a different user. I am using a foreign key in products table (user_id) and every command has a user to inspect it.
This is my function in the controller:
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]);
}
This is my route :
Route::get('/product/{id}', [ 'uses' => 'CommandeUserController@getProduct', 'as' => 'product.single' ]);
and this is the view:
@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
2
Answers
Hello I changed the getProduct() to and it works:
and I used in the form a new store funtion for the command:
Since I feel weird that your error message doesn’t take any value from your request, try to add your $commande variable to a standard bracket ‘()’ to your last model initiation.