JOHNMAC's avatar

post method in route says 404 | not found

hello I make a form to upload pics in Laravel but when I open the form then it shows error: 404 | not found using post in route, and if I change it to get method then it shows "Invalid argument supplied for foreach()" blade:

    <form method="POST" action="{{ route('admin.product.alternateimages') }}" enctype="multipart/form-data" class="add-new-post">
                  @csrf
                <strong class="text-muted d-block mb-2 mt-5">Upload Product Image</strong>
                <div class="input-group mb-3">
                  <div class="input-group input-group-seamless">
                    <input type="file" name="product_alt_img[]" class="form-control mb-2 btn btn-sm btn-outline-primary mr-1 @error('product_alt_img') is-invalid @enderror" value="{{ old('product_alt_img') }}" id="" placeholder=""> </div>
                    @error('product_image')
                      <div class="small text-danger">{{ $message }}</div>
                    @enderror
                </div>
              </form>

route:

   Route::post('/product/{product}','ProductController@alternateimages')->name('product.alternateimages');

controller:

   public function alternateimages(Request $request)
   {
    $altimgs = new Product;
    dd($request->all);
    $altimgs->product_id = $request->product_id;
    $files = $request->file('product_alt_img');
    foreach ($files as $file) {
      $images = $file->getClientOriginalName();
      $file->move(public_path('images/backend_images/product_images'), $images);
      $altimgs->product_alt_img = $images;
    }
    $altimgs->save();

    return redirect()->back()->with('flash_message_success', 'Product Images has been added successfully');   
   }
0 likes
16 replies
ashkan90's avatar

the route name is not correct on form action.

Route::post('/product/{product}','ProductController@alternateimages')->name('product.alternateimages');

this is your route name product.alternateimages and you're sending your form to admin.product.alternateimages so laravel cannot understand what you're trying to do :d

just change admin.product.alternateimages to product.alternateimages on your form

Snapey's avatar

Your route expects a product to be provided

Route::post('/product/{product}'

but in the form, you don't send product

assuming you have $product available in the form (to know which product it is an image for) change the action

    action="{{ route('admin.product.alternateimages', $product) }}"

(I am also assuming that the route is inside a route group with admin prefix ?)

Snapey's avatar

What's wrong with my answer. Do I need to explain more?

JOHNMAC's avatar

it will be highly preferable if u explain it with documentation link

Snapey's avatar

There is no documentation link for bad code!

Route;

Route::post('/product/{product}'
                       ^^^^^^^

your route is expecting a parameter to be passed

In addition, it probably also clashes with your product resource route

Change the route;

Route::post('/product/{product}/altimage','ProductController@alternateimages')->name('product.alternateimages');

Then fix the Form

    <form method="POST" action="{{ route('admin.product.alternateimages', $product) }}" enctype="multipart/form-data" class="add-new-post">

Then fix the controller

    public function alternateimages(Request $request, Product $product)
    {
        (dd($product, $request->all());
    }

And don't you dare come back and just say 'doesn't work'

Check what your controller receives then work on saving image(s) as a separate issue

1 like
alishahUK's avatar

Let's break it down

// In your route you are expecting {product} to be passed in
Route::post('/product/{product}','ProductController@alternateimages')->name('product.alternateimages');

However, in the form itself you aren't passing product at all ..

    <form method="POST" action="{{ route('admin.product.alternateimages') }}" enctype="multipart/form-data" class="add-new-post">

Then in the controller itself, you are trying to access product off $request ... So whats the solution,

    <form method="POST" action="{{ route('product.alternateimages' , [ // call product.alternativeimages as 
 //you have named it in your route 
             'product' => $product
     ]) }}" enctype="multipart/form-data" class="add-new-post">

documentation link : https://laravel.com/docs/master/urls#urls-for-named-routes

Then in your controller,

public function alternateimages(Request $request, Product $product ) {
  // logic
}

Explore these links to fully understand

https://laravel.com/docs/master/routing#implicit-binding https://laravel.com/docs/5.8/controllers#dependency-injection-and-controllers

Hope that helps

alishahUK's avatar

Hey @snapey , the whole point of this forum is to help and be gracious :). Using something like Don't dare to come back or bad code only intimidate others. Hope you find my feedback as constructive feedback

1 like
JOHNMAC's avatar

sory still same , and I hv already asked that if I am at wrong way then u can just tell me whats the preferable way, I just fix it by making a new "model -mcr" but now the page is slow

Snapey's avatar

I have given you the actual code to use. Not sure what else I can do?

Snapey's avatar

@alishahuk thanks for repeating what I have already written (twice)

Sorry if you don't like my tone, but after spending considerable time reviewing and constructing answer, I don't like getting

still same

and then

not working yet

and then

sory still same

If you don't think I put in the effort. Check the XP and best replies.

JOHNMAC's avatar

its ok u done a great job, all is fine, u r a best programmer, even most of my solutions are provided by you, the new model is working I make a new model because I am weak in relational dbms, I was working on it since last 2 days but its was not working, I don't know why its not working so I make a new "-mcr"

anibabbar's avatar

Just loved this - "There is no documentation link for bad code!"

humbleguidant's avatar

Dude, you dont have to be rude and arrogant when someone is trying to find answers. Just because we are not programming experts like you yet doesnt mean you have to be condescending. This is what I hate about the computer science industry, some computer programmers expect people to kiss their butts just because they are good at programming. Be humble old man.

Please or to participate in this conversation.