skip to Main Content

I’m going insane. The entire code looks correct, but I’m still getting this error. But somewhere, something must be going wrong.

Controller

public function new(Request $request){
    $request->validate([
        'name' => 'required|max:255|min:10|string',
        'price' => 'numeric|max:255|required',
        'stock' => 'required|numeric|max:255',
        'description' => 'required|string|max:5000|min:10',
        'picture' => 'required|image',
        'picsupp1' => 'image',
        'picsupp2' => 'image',
        'picsupp3' => 'image',
        'delivery' => 'required|string',
        'category' => 'required|string',
    ]);

    $crypt = openssl_random_pseudo_bytes(30);
    $token = bin2hex($crypt);

    $product = new Product;
    $product->name = $request->input('name');
    $product->price = $request->input('price');
    $product->stock = $request->input('stock');
    $product->description = $request->input('description');
    $product->picture = request('picture')->store('articles','public');
    $product->locked = 0;
    if(request('picsupp1')){
        $product->picsupp1 = request('picsupp1')->store('articles','public');
    }else{
        $product->picsupp1 = '';
    }
    if(request('picsupp2')){
        $product->picsupp2 = request('picsupp2')->store('articles','public');
    }else{
        $product->picsupp2 = '';
    }
    if(request('picsupp3')){
        $product->picsupp3 = request('picsupp3')->store('articles','public');
    }else{
        $product->picsupp3 = '';
    }
    $product->delivery_token = $request->input('delivery');
    $product->category_token = $request->input('category');
    $product->vendor_id = Auth::user()->id;
    $product->token = $token;

    $product->save();

    return redirect(route('index'))->with('success','Product added with success !');
}

Blade

<div class="col-lg-9">
  <div class="card mb-4">
    <div class="card-header bg-dark text-white">New product</div>
    <div class="card-body">
        <form method="POST" action="{{ route('product.action.new') }}" enctype="multipart/form-data" >
            @csrf
            @method('PUT')
            <div class="form-floating mb-4">
                <input placeholder="Enter product name" type="text" name="name" class="form-control @error('name') is-invalid @enderror" id="floatingInputGroup1" >
                <label for="floatingInputGroup1">Name <span style="color:red">*</span></label>
                @error('name')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="form-floating mb-4">
                <select class="form-control mb-4 @error('category') is-invalid @enderror" name="category">
                    @foreach(AppModelsMarketCategory::allCategorie() as $item)
                        @if(!empty($item['children']))
                        <option value="{{ $item['token'] }}" >{{ $item['name'] }} </option>
                            @foreach ($item['children'] as $children)
                                <option value="{{ $children['token'] }}" >-- {{ $children['name'] }}</option>
                            @endforeach
                        @else
                            <option value="{{ $item['token'] }}" >{{ $item['name'] }}</option>
                        @endif
                    @endforeach
                </select>
                <label for="floatingInputGroup1">Category <span style="color:red">*</span></label>
                @error('category')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="input-group">
                <div class="form-floating mb-4 me-4">
                    <input placeholder="Enter product price" type="text"  name="name" class="form-control @error('price') is-invalid @enderror" id="floatingInputGroup1" >
                    <label for="floatingInputGroup1">Price <span style="color:red">*</span></label>
                    @error('price')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </div>

                <div class="form-floating mb-4">
                    <input placeholder="Enter product stock" type="text"  name="name" class="form-control @error('stock') is-invalid @enderror" id="floatingInputGroup1" >
                    <label for="floatingInputGroup1">Stock <span style="color:red">*</span></label>
                    @error('stock')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                </div>
            </div>
            <div class="form-floating mb-4">
                <textarea placeholder="Enter product description" style="height:280px" name="description" class="form-control mb-4 @error('description') is-invalid @enderror"></textarea>
                <label for="floatingInputGroup1">Description <span style="color:red">*</span></label>
                @error('description')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <label for="floatingInputGroup1">Picture <span style="color:red">*</span></label>
            <input type="file" name="picture" class="form-control mb-4 @error('picture') is-invalid @enderror" />
            @error('picture')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror

            <div class="mb-0">
                <label class="form-label">Picture supp 1</label>
                <input type="file" name="picsupp1" class="form-control mb-4 @error('picsupp1') is-invalid @enderror" />
                @error('picsupp1')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
                @enderror
            </div>
            <div class="mb-0">
                <label>Picture supp 2</label>
                <input type="file" name="picsupp2" class="form-control mb-4 @error('picsupp2') is-invalid @enderror" />
                @error('picsupp2')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
                @enderror
            </div>
            <div class="mb-0">
                <label>Picture supp 3</label>
                <input type="file" name="picsupp3" class="form-control mb-4 @error('picsupp3') is-invalid @enderror" />
                @error('picsupp3')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
                @enderror
            </div>
            <div class="form-floating mb-4">
                <select class="form-control mb-4 @error('delivery') is-invalid @enderror" name="delivery">
                    @foreach(AppModelsMarketDelivery::where('user_id', Auth::user()->id)->get() as $delivery)
                        <option value="{{ $delivery->token }}">{{ $delivery->name }}</option>
                    @endforeach
                </select>
                <label>Delivery <span style="color:red">*</span></label>
                @error('delivery')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
                @enderror
            </div>
            <button type="submit" class="btn btn-success">Add product</button>
        </form>
    </div>
  </div>
</div>

Route

Route::post('/vendor/product/action/new', [AppHttpControllersMarketProductController::class, 'new'])->name('product.action.new');

Does anybody maybe see somewhere the error or mistake, been now busy since yesterday with this and never encountered this issue before.

3

Answers


  1. One issue is with the HTTP method you use in your form and the one defined in your route. In your form, you’re using @method('PUT'), which is trying to send a PUT request. However, in your route definition, you’re expecting a POST request.

    Remove the @method('PUT') line from your form to fix this. When you submit the form, it will send a POST request, which matches the HTTP method defined in your route.

    <form method="POST" action="{{ route('product.action.new') }}" enctype="multipart/form-data" >
        @csrf
        <!-- rest of your form fields -->
    </form>
    

    Secondly, the word new is a reserved keyword in PHP for creating new objects. Using new as a function name will result in a syntax error. You should choose a different name that is not a reserved keyword for your function. Typically, for this kind of action in Laravel, you would use "store" as your function and route name.

    Login or Signup to reply.
  2. Looks like your route is defined to only accept POST so if you try to PUT it will fail.

    Try updating your route:

    Route::put('/vendor/product/action/new', [AppHttpControllersMarketProductController::class, 'new'])->name('product.action.new');

    Login or Signup to reply.
  3. some input names are wrong, for example the stock input has a name=’name’ and it should be name=’stock’. this make validation to fail and the error not to be displayed i believe.
    i did not check all of them there may be more.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search