简体   繁体   中英

Controller Method not being called Laravel

I have a Form that contains a Dropdown and a Submit button. Like so:

View path: webmasters/filters.blade.php

 {{ Form::open() }}
  {{ Form::select('filt', $Dropdown, 2) }}
  {{ Form::Submit('Filter') }}
 {{ Form::close() }}

And a controller that populates the Dropdown with values queried from a DB. Like so:

Controller name: WebController.php

 class WebController extends BaseController {
   public function getFilters() {
     $filters = Dropdown::getFilters();
     return View::make('webmasters.filter',['Dropdown'=>$filters]);
   }

   public function postFilters() {
     $filt = Input::get('name'); // getting the value of the select
     $filters = Dropdown::getFilters();
     $query = DB::table('webmasters.top_pages')->where('filter',$filt)->limit(20)->get();
     return View::make('webmasters.filter', array('query'=>$query),['Dropdown'=>$filters]);
   }

 }

Here is my route:

 Route::resource('topPage', 'WebController@getFilters');

getFilters is a model method that queries the DB for the values that come into the dropdown.

EDIT

My View file:

 {{ Form::open() }}
 <p></p>
 <table class='liste' style='margin-left: 0px;' cellpadding='5'> 
 {{ Form::select('name', $Dropdown) }}
 <div>
 {{ Form::Submit('Filter') }}
 </div>
    <tr>
        <td style='background-color: #426bb3; color: white; font-weight: bold; width:16%;'>Datum</td>
        <td align='left' style='background-color: #426bb3; color: white; font-weight: bold; width:12%;'>Page</td>
        <td align='left' style='background-color: #426bb3; color: white; font-weight: bold; width:12%;'>Kategorie</td>

    </tr>   
    @foreach($Webmasters as $topPages)
    <tr>
        <td> {{$topPages->date}} </td>              
        <td> {{$topPages->page}} </td>        
        <td> {{$topPages->category}} </td>  
        </tr>     
    @endforeach   
    </table><br>  
{{ Form::close() }} 

I would like to call a controller method upon submitting the form so that the said method queries another DB and returns a table (on the same page as the dropdown and submit button) based on the selected value of the dropdown.

Since the default Form created using Form::post() is using POST, i figured i could access the value of the select using the second method in my controller postFilters() . This method goes ahead and uses the value as a where-clause as it queries anther DB and passes the results onto a View. The Problem is, the View is not loading. I suppose i'm doing something wrong on the routing?? Can someone help?

Your controller isn't passing to the view what the view is expecting. You inject query and the view wants $Webmasters . Also the second array with Dropdown you added won't work. This should fix the issue:

public function postFilters() {
    $filt = Input::get('name'); // getting the value of the select
    $filters = Dropdown::getFilters();
    $query = DB::table('webmasters.top_pages')->where('filter',$filt)->limit(20)->get();
    return View::make('webmasters.filter', ['Webmasters'=>$query, 'Dropdown'=>$filters]);
}

Edit

Also you're using Route::controller with a controller function. You have to pass the controller class.

Route::controller('top_page', 'WebController');

It's important that you now use top_page/filters as URL for accessing the form. (and posting it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM