skip to Main Content

Im using an achor to click a button to navigate to other page. However, it doesnt work and return a 404 error not found.

My destination blade is : enter image description here

inventory.blade.php:

<a class="d-xl-flex align-items-xl-center" href="{{ url('inventory/add')}}" style="padding-right: 0px;margin-right: 15px;"><button class="btn btn-primary d-xl-flex align-items-xl-center" type="button" style="margin-right: -16px;margin-bottom: 13px;margin-top: 7px;border-color: rgb(162,138,138);background: rgb(0,0,0);padding-bottom: 0px;padding-top: 1px;margin-left: -26px;">ADD PRODUCT</button></a>


controller: NavBar.php:

public function inv(){
        return view('inventory');
    }
public function invadd(){
        return view('inventory-add');
    }

web.php:
enter image description here

I tried moving the inv() in another controller page and it’s not already viewable or accessible by the routing. Is it because of referencing thing? I think my controller cannot access the blades in the resources/views folder.

5

Answers


  1. Chosen as BEST ANSWER

    I'm using my old code with url and it worked. I just prompt this in terminal:

    php artisan route:clear
    php artisan cache:clear
    

  2. You have an anchor tag surronding a button, which doesn’t make sense. use either one or the other. That said, you can –

    Give your routes a name

    Route::get('inventory-add')->name('inventory-add');
    

    Then link to it by using the route helper.

    <a class="d-xl-flex align-items-xl-center" 
       href="{{ route('inventory-add')}}" 
       style="padding-right: 0px;margin-right: 15px;">
       ADD PRODUCT
    </a>
    

    You also seem to be trying POST to a route using an anchor tag, which also doesn’t make sense. You probably want a form with a submit button which will sent the data using POST with the data from the form.

    Login or Signup to reply.
  3. try again, like this:

    Route::get('inventory/add')->name('inventoryAdd');
    

    get() params corresponding browsers url.

    name() You can use it like this: route('inventoryAdd').

    Login or Signup to reply.
  4. As per your comment:

    What Im trying to make is a button of an "add product" then it will navigate to another page to enter the data. Tried your solution it says "Route [inventory-add] not defined."

    Route::controller(NavBar::class)->group(function () {
        Route::get('/inventory/add', 'create')->name('product.create');
        Route::post('/inventory/add', 'invadd')->name('product.store');
    });
    

    Controller method

    public function create()
    {
        return view('inventory-add');
    }
    
    public function invadd(Request $request)
    {
        // your logic to save the product here
    }
    

    and in your blade

    <a href="{{ route('product.create')}}">
       Add product
    </a>
    

    You are trying to open a link that is assigned to a post route, that’s why you’re getting 404 error, common oversight actually.

    Login or Signup to reply.
  5. Have you tried putting a slash in the front like this:

    href="{{ url('/inventory/add')}}"
    

    For example you are currently on the location:

    http://example.com/anotherpage
    

    And you have this

    href="{{ url('inventory/add')}}"
    

    Clicking on that link, you will go to:

    http://example.com/anotherpage/inventory/add
    

    So you really have to put a slash character in front of inventory so that you will go to

    http://example.com/inventory/add
    

    Regardless of your current location on your project.

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