Ad

Laravel Running Function From Button In Blade

- 1 answer

I am having an issue getting a button in a blade file to properly run a function in a controller, when i click the button i get a methodnotallowedexception and have not been able to figure out what is not setup correctly

To my knowledge this is written like another button i have for a different blade and controller, unless i am overlooking something, which is why i am here. if you need other code let me know.

first my routes:

Route::post('/viewPatient/{user}/discharge', '[email protected]')->name('patients.discharge');
Route::post('/viewPatient/{user}/reAdmitt', '[email protected]')->name('patients.reAdmitt');  
Route::post('/viewPatient/{user}/reAdmitted', '[email protected]')->name('patients.reAdmitted');

next the controller functions

public function discharge(User $user)
    {
        $user->discharged = true;
        $user->discharged_date = now();
        $user->current_facility_id = null;
        $user->save();
        // find all the users documents that are not historical
        $documents = Document::where('user_id', $user->id)->where('historical', false)->get();
        // mark them all as historical
        foreach($documents as $document){
            $document->historical = true;
            $document->save();
        }

        return $this->index(); 
    }

    public function reAdmitt(User $user)
    {
        $user->discharged = false;
        $user->readmitting = true;
        $user->reAdmission_start = now();
        $user->save();

        return $this->index(); 
    }

    public function reAdmitted(User $user)
    {
        $user->discharged = false;
        $user->readmitting = false;
        $user->reAdmitted_on = now();
        $user->save();

        return $this->index(); 
    }

and finally the buttons themselves

<button><a target="_blank" rel="nofollow noreferrer" href="{{route('patients.reAdmitt', $user)}}">Readmitt Patient</a></button>
<button><a target="_blank" rel="nofollow noreferrer" href="{{route('patients.reAdmitted', $user)}}">Patient Signed reAdmission</a></button>
<button><a target="_blank" rel="nofollow noreferrer" href="{{route('patients.discharge', $user)}}">Discharge Patient</a></button>

the expected result is that it should run the function and update the database, i know the function to discharge works because i accidentally changed the route to a get and it ran when the page loaded and marked one of the patients as discharged.

Ad

Answer

You are not submitting any form. So you don't need to use post routes. <a> tag is for get routes. So change your routes as

Route::get('/viewPatient/{user}/discharge', '[email protected]')->name('patients.discharge');
Route::get('/viewPatient/{user}/reAdmitt', '[email protected]')->name('patients.reAdmitt');  
Route::get('/viewPatient/{user}/reAdmitted', '[email protected]')->name('patients.reAdmitted')

;

Ad
source: stackoverflow.com
Ad