php - Controller 方法未被调用 Laravel

标签 php html mysql forms laravel

我有一个包含下拉菜单和提交按钮的表单。就像这样:

查看路径:webmasters/filters.blade.php

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

还有一个 Controller ,它使用从数据库查询的值填充下拉列表。就像这样:

Controller 名称: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]);
   }

 }

这是我的路线:

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

getFilters 是一种模型方法,用于在数据库中查询下拉列表中的值。

编辑

我的 View 文件:

 {{ 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() }} 

我想在提交表单时调用 Controller 方法,以便该方法查询另一个数据库并根据下拉列表的选定值返回一个表(与下拉列表和提交按钮在同一页面上)。

由于使用 Form::post() 创建的默认表单使用的是 POST,我想我可以使用 Controller 中的第二种方法来访问选择的值 postFilters()。该方法在查询另一个数据库并将结果传递到 View 时继续使用该值作为 where 子句。 问题是, View 未加载。我想我在路由上做错了什么? 有人可以帮忙吗?

最佳答案

您的 Controller 没有将 View 期望的内容传递给 View 。您注入(inject) query 并且 View 需要 $Webmasters。此外,您添加的带有 Dropdown 的第二个数组也将不起作用。这应该可以解决问题:

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]);
}

编辑

此外,您还使用带有 Controller 功能的 Route::controller 。您必须传递 Controller 类。

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

现在使用 top_page/filters 作为访问表单的 URL 非常重要。 (并发布)

关于php - Controller 方法未被调用 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27250934/

相关文章:

php - 如何使用 REGEX php 从一系列 CSS 声明中提取十六进制值

php - 查找添加的好友 MySQL(关系型)

javascript - 动态命名部分 div 以备后用?

javascript - 运行许多查询时,Chrome 与 Web SQL 中的问题/错误?

html - 网站的水平时间表栏

c# - 从 C# 连接到 Amazon EC2 MySQL 时出错

php - 如何让我的聊天分区显示在彼此下方

php - 遇到 PHP 错误严重性 : Notice Message: Undefined offset: 0

MySQL如果存在则插入id否则插入到两个表中

php - Joomla mysql 查询无法正常工作