0

When I try to get a product and command it I get "Illuminate\Database\QueryException 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
  • Are you actually filling in the `` for `name`? You don't have any kind of validation, so if you don't fill it in, and your database doesn't allow `null`, you're gonna get that error. Also, you shouldn't use a `Route::get()` to create something. Take a look at Laravel's Resource Controllers to see the typical approach: https://laravel.com/docs/9.x/controllers#resource-controllers – Tim Lewis Nov 21 '22 at 21:53
  • I am trying to fill the name but the error comes before the form to fill – Malik Kadri Nov 21 '22 at 23:11
  • 1
    why is `getProduct` creating a record? you should have a POST route that is creating that record, the GET route that returns a view should just be gathering needed data for the view that is returned – lagbox Nov 22 '22 at 00:36
  • Well of course. If the Route you're using to display the form is also expecting input from the form, how would you expect that to work? You need to separate your Routes. Again, look at that Resource Controller I linked; it shows you how a Controller should use separate Routes, one `GET` for displaying the Create form, one `POST` for handling the form Submissions, another `GET` and `POST` for editing, etc. etc. – Tim Lewis Nov 22 '22 at 02:08

2 Answers2

0

Hello I changed the getProduct() to and it works:

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

    $product = Product::find($id);


    return view('product', ['product' => $product]);

}

and I used in the form a new store funtion for the command:

 public function store(StoreProductRequest $request)
{

            $user_id=auth()->user()->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=$user_id;
            $commande->save();


       return redirect('/commandeuser/confirm')->with('status', 'commande ajoutée!');

}
-1

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.

$commande = new AppComande();
  • Well I changed the creation of command to an other function now i am getting: Property [name] does not exist on this collection instance. – Malik Kadri Nov 22 '22 at 15:42
  • @MalikKadri If you have a new problem, please ask a new question. But, with that being said, please search Stackoverflow for that error first; you're not the only one who's had that error: https://stackoverflow.com/questions/70030106/property-id-does-not-exist-on-this-collection-instance, https://stackoverflow.com/questions/41366092/property-title-does-not-exist-on-this-collection-instance, https://stackoverflow.com/questions/63219872/property-role-does-not-exist-on-this-collection-instance. You're likely using `->get()` when you should be using `->first()`. – Tim Lewis Nov 22 '22 at 20:27
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Albert Logic Einstein Nov 27 '22 at 15:01