【问题标题】:Laravel - Dropdown filter gives error on changeLaravel - 下拉过滤器在更改时给出错误
【发布时间】:2020-10-20 04:06:05
【问题描述】:

在我的 Laravel-5.8 中,我有以下代码:

public $filters = [
    "all" => "all",
    "logged_in" => "logged in users",
    "not_logged_in" => "not logged in users",
];

public function index($id = "")
{
    $userCompany = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;
    $userId = Auth::user()->id;
    $employeecode = Auth::user()->employee_code;
    
    $datax = User::where('last_login_at', '>', today()->subDays(30))
            ->where('hr_status', 0)
                ->where('company_id', $userCompany)
                ->orderBy('last_login_at', 'desc')
                ->get();
    dd($datax);
    
    if ($id == 'all') {
        $data = User::where('hr_status', 0)->where('company_id', $userCompany)->orderBy('last_login_at', 'desc')->get();
    } 
    elseif ($id == "") {
        $data = User::where('hr_status', 0)->where('company_id', $userCompany)->where('active', 0)->orderBy('last_login_at', 'desc')->get();
    } 
    elseif ($id == "logged_in") {
        $data = User::where('last_login_at', '>', today()->subDays(30))
            ->where('hr_status', 0)
                ->where('company_id', $userCompany)
                ->orderBy('last_login_at', 'desc')
                ->get();
    }        
    elseif ($id == "not_logged_in") {
        $data = User::where('last_login_at', '<', today()->subDays(30))
                ->orWhereNull('last_login_at')
            ->where('hr_status', 0)
                ->where('company_id', $userCompany)
                ->orderBy('last_login_at', 'desc')
                ->get();
    }        

    return view('report.report_user_login_logs.index')
            ->with('employees', $data)
            ->with('filters', $this->filters)
            ->with('selectedFilter', $id);
}

查看刀片:

查看/报告/report_user_login_logs/index.blade.php

            <div class="form-group">
            <select class="form-control" id="filter">
                <option value="select">Select Search Criteria</option>
                @foreach($filters as $filter)
                <option value="{{$filter}}" @if($filter==$selectedFilter) selected @endif>{{ucfirst(trans($filter))}}</option>
                @endforeach
            </select>
            </div>
            <tbody>
                @foreach($employees as $key => $employee)
                <tr>
                    <td>
                        {{$employee->employee_code}}
                    </td>  
                    <td>
                        {{$employee->first_name}} {{$employee->last_name}}
                    </td>  
                    <td>
                        {{$employee->email}}
                    </td>    
                    <td>
                        {{$employee->last_login_at}}
                    </td>                         
                </tr>
                @endforeach
            </tbody>


<script type="text/javascript">
    $(document).ready(function () {
        $("#filter").change(function(e){
            if ($(this).val()=== "select" ){

                var url = "{{route('report.report_user_login_logs.index')}}/"
            }
            else{
                var url = "{{route('report.report_user_login_logs.index')}}/" + $(this).val();
            }

            if (url) {
                window.location = url;
            }
            return false;
        });
    });
</script>

路由/web.php

Route::group(['prefix' => 'report', 'as' => 'report.', 'namespace' => 'Report', 'middleware' => ['auth']], function () {

    Route::resource('report_user_login_logs', 'ReportUserLoginLogsController');

});

然后 php artisan route:list 给出

|        | GET|HEAD                               | report/report_user_login_logs                                                              | report.report_user_login_logs.index                                        | App\Http\Controllers\Report\ReportUserLoginLogsController@index                                        | web,auth           |
|        | POST                                   | report/report_user_login_logs                                                              | report.report_user_login_logs.store                                        | App\Http\Controllers\Report\ReportUserLoginLogsController@store                                        | web,auth           |
|        | GET|HEAD                               | report/report_user_login_logs/create                                                       | report.report_user_login_logs.create                                       | App\Http\Controllers\Report\ReportUserLoginLogsController@create                                       | web,auth           |
|        | DELETE                                 | report/report_user_login_logs/{report_user_login_log}                                      | report.report_user_login_logs.destroy                                      | App\Http\Controllers\Report\ReportUserLoginLogsController@destroy                                      | web,auth           |
|        | PUT|PATCH                              | report/report_user_login_logs/{report_user_login_log}                                      | report.report_user_login_logs.update                                       | App\Http\Controllers\Report\ReportUserLoginLogsController@update                                       | web,auth           |
|        | GET|HEAD                               | report/report_user_login_logs/{report_user_login_log}                                      | report.report_user_login_logs.show                                         | App\Http\Controllers\Report\ReportUserLoginLogsController@show                                         | web,auth           |
|        | GET|HEAD                               | report/report_user_login_logs/{report_user_login_log}/edit                                 | report.report_user_login_logs.edit                                         | App\Http\Controllers\Report\ReportUserLoginLogsController@edit                                         | web,auth           |

我将使用下拉列表作为过滤器来显示表格上的数据。 doropdown onchange:

            <select class="form-control" id="filter">
                <option value="select">Select Search Criteria</option>
                @foreach($filters as $filter)
                <option value="{{$filter}}" @if($filter==$selectedFilter) selected @endif>{{ucfirst(trans($filter))}}</option>
                @endforeach
            </select>

给出错误 500

当我检查错误日志时,我得到了这个错误:

production.ERROR: 方法 App\Http\Controllers\Report\ReportUserLoginLogsController::show 不存在

我哪里弄错了?我要纠正它吗?

谢谢

【问题讨论】:

    标签: laravel


    【解决方案1】:

    production.ERROR: 方法 App\Http\Controllers\Report\ReportUserLoginLogsController::show 不存在

    表示控制器不包含show() 函数,Route::resource('report_user_login_logs', 'ReportUserLoginLogsController'); 加载每个可用的资源操作,但当它到达 show() 时会抛出错误,因为它不存在。

    您可以使用Partial Resource Routes排除某些操作:

    Route::resource('report_user_login_logs', 'ReportUserLoginLogsController',
                    ['except' => ['show']]);
    

    或者只包括某些操作:

    Route::resource('report_user_login_logs', 'ReportUserLoginLogsController',
                    ['only' => ['index']]);
    

    【讨论】:

    • @jewishmodes - 错误更改为生产。错误:未定义变量:数据
    • @mikefolu Read this 它解决了你的问题。
    猜你喜欢
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多