Zeekstem
Zeekstem

Reputation: 838

Laravel route returning error 404 when attempt is made to pass value to controller function

I have a button in my blade like this

@can('customer_show')
                <a class = "btn btn-primary" href = "{{ route('admin.loan-applications.showCustView', $loanApplication->user_id) }}">
                    View Applicant 
                </a>
            @endcan

And this is the route:

Route::get('loan-applications/{loan_application}/showCustView', 'loanApplicationsController@showCust')->name('loan-applications.showCustView');

And in my controller, i did:

public function showCust(LoanApplication $loanApplication)
{
    
    
    $customerInformation = customerInfoModel::where('Cust_id', $loanApplication->user_id));
     
    return view('admin.loanApplictions.showCustView', compact(['customerInformation', 'loanApplication']));
}

What i am trying to do is fetch the row from the database attached to customerInfoModel where the the Cust_id field equals the loanApplication->user_id of the loan being viewed currently in the blade above. When the button "view Applicant" is hit, i get an error 404 page. Why is that so?

Upvotes: 0

Views: 1439

Answers (3)

Ntiyiso Rikhotso
Ntiyiso Rikhotso

Reputation: 720

I have also encountered a similar issue with route model binding where I was unable to access a specific route even though it was properly defined. After extensive debugging, I discovered that the problem was caused by missing headers. It is important to include the following headers in your request to prevent a 404 error:

Accept: application/json
Content-Type: application/json

Including these headers ensures that the request is treated as a JSON request and that the server knows how to handle and interpret the data correctly.

Upvotes: 0

Saravana Sai
Saravana Sai

Reputation: 113

Check it out the route list using this command

php artisan route:list

//this command will show all the routes in your application

If your route not listed on that route list checkout for routes with same Url on your route manually on routes file.

if you found change the url & use this command to clear cache

php artisan optimize:clear

i have found the comment of you on last answer. check out for route model binding . i think you have to add a

public function showCust( $loanApplicationCustId)
{
    
    $customerInformation = customerInfoModel::where('Cust_id', $loanApplicationCustId))->first();
     
    return view('admin.loanApplictions.showCustView', compact(['customerInformation', 'loanApplication']));
}

It should be like this .. i hope it works for you...... else share your project git repo link

Upvotes: 2

Alya Al Siyabi
Alya Al Siyabi

Reputation: 56

I think it should be like following:

@can('customer_show')
                <a class = "btn btn-primary" href = "{{ route('loan-applications.showCustView', $loanApplication->user_id) }}">
                    View Applicant 
                </a>
            @endcan

try that

Upvotes: 0

Related Questions