I am getting 404 not found

  Wed 07 / 06 / 2023

  Posted by: OG Designs

Category : Laravel

Tags : laravel , php , php laravel , backend

Post Thumbnail
Description

I am editing my form, but once I click on Update, it will show me  404 NOT | FOUND.

When i click on edit the editing is working (/admin/products/18/edit) but is not updating when i click on Update



View Code
                        public function update(ProductFormRequest $request, int $product_id)
    {
        
        $validatedData = $request->validated();
        $product = Category::findOrFail($validatedData['category_id'])
                ->products()->where('id',$product_id)->first();
        if($product)
        {
            $product->update([
                'category_id' => $validatedData['category_id'],
                'name' => $validatedData['name'],
                'slug' => Str::slug($validatedData['slug']),
                'brand' => $validatedData['brand'],
                'small_description' => $validatedData['small_description'],
                'description' => $validatedData['description'],
                'original_price' => $validatedData['original_price'],
                'selling_price' => $validatedData['selling_price'],
                'quantity' => $validatedData['quantity'],
                'trending' => $request->trending == true ? '1' : '0',
                'status' => $request->status == true ? '1' : '0',
                'meta_title' => $validatedData['meta_title'],
                'meta_keyword' => $validatedData['meta_keyword'],
                'meta_description' => $validatedData['meta_description'],
           ]);

           if ($request->hasfile('image')) {
            $uploadPath = 'uploads/products/';

            $i = 1;
            foreach ($request->file('image') as $imageFile) {
                $extention = $imageFile->getClientOriginalExtension();
                $filename = time() . $i++ . '.' . $extention;
                $imageFile->move($uploadPath, $filename);
                $finalImagePathName = $uploadPath . $filename;

                $product->productImages()->create([
                    'product_id' => $product->id,
                    'image' => $finalImagePathName,
                ]);
            }
        }

        return redirect('/admin/products')->with('message', 'Product Updated Successfully');

        }

        else
        {
            return redirect('/admin/products')->with('message', 'No such Product id found');
        }
    }
                
  Answered by CDL

In the Controller update method on the 2nd line you are using the findOrFail method, so if the category wouldn't found it throws 404 exceptions.

~~~

$product = Category::findOrFail($validatedData['category_id'])

~~~

How to solve it.

Use only the find method because you are catching the errors manually.

~~~

$product = Category::find($validatedData['category_id'])

~~~

  Comment   Share
0 Likes   1 Comment


comment-author
OG Designs Author

Still not updating Same 404 error message



Reply


commented 1 year ago
comment-author
Hadayat Niazi Super Admin

Makes sure your path is correct because you are calling it as admin/product/18 but your route don't have admin prefix so how it will locate the exact route.