hanif-king's avatar

laravel method not allowed exception with form attribute target="_blank"

My codes result on view woks well and has no error but after i am generating reports of my records from a form submitted from one page and result of the submitted form to another page. it display data well and no error but when i am clicking on pagination button it gives the error of :

 MethodNotAllowedHttpException in RouteCollection.php line 251:

my form :

``page:generateReport``

<form method="post" target="_blank" action="{{route('MainReport')}}">
    {{csrf_field()}}
//form fields 

<button type="submit">submit</button>
</form>

my route:

Route::post('MainReport','ReportController@MainReporting')->name('MainReport');

my controller:

public function MainReporting(Request $request){
    $this->validate($request,[
        'ToDate'=>'Required',
        'fromDate'=>'Required'
    ]);
    $DataOrderNotSpecial=null;
    $DataOrderSpecial=null;
    if($request->OilStationManazada=='on'){ //condition 
        $DataOrderSpecial=Order::Where(function ($query) use($request){
            $query->whereBetween('order_Date', [$request->fromDate, $request->ToDate]);
        })->Where('subcontract_id','!=',null)->paginate(10);
        $DataOrderNotSpecial=Order::Where(function ($query) use($request){
            $query->whereBetween('order_Date', [$request->fromDate, $request->ToDate]);
        })->Where('subcontract_id','=',null)->paginate(10);
        return view('pages/admin/reporting/MainReporting',compact('DataOrderSpecial','DataOrderSpecial'));
    }
// as you see i am sending form from a page called `` generateReport ``  to another page called `` MainReporting `` has no error on displaying result but only i am pressing the button paginates it gives my the error. please help.

i know i am messing something with pagination. but dont know what is that.

0 likes
3 replies
skliche's avatar

Keep in mind that the links() method to display the pagination results creates simple HTML links. Clicking on one of those links will generate a GET request. Your route accepts POST requests only. Sending a get request to that route gives you the method not allowed exception.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

As mentioned by @Skliche pagination links are GET

As a rule, always redirect after a post, rather than return a view.

You should change this whole method to being a GET route rather than POST, with the querystrings appearing in the URL. You will also need to append the query values to the links because Laravel needs to perform the same database query on every page.

here; https://laravel.com/docs/5.6/pagination#displaying-pagination-results

see Appending to pagination links

Please or to participate in this conversation.