Home > Net >  laravel 9 anchor button returning error 404
laravel 9 anchor button returning error 404

Time:10-27

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  href="{{ url('inventory/add')}}" style="padding-right: 0px;margin-right: 15px;"><button  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.

CodePudding user response:

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.

CodePudding user response:

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  
   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.

CodePudding user response:

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

CodePudding user response:

try again, like this:

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

get() params corresponding browsers url.

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

  • Related