0
votes

In my Laravel-5.8, I have this code:

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

view blade:

view/report/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>

route/web.php

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

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

});

Then php artisan route:list gives

|        | 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           |

I am to use the dropdown as filter to display the data on the table. The 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>

gives error 500

When I check error log, I got this error:

production.ERROR: Method App\Http\Controllers\Report\ReportUserLoginLogsController::show does not exist

Where dis I get it wrong? And do I correct it?

Thank

1

1 Answers

0
votes

production.ERROR: Method App\Http\Controllers\Report\ReportUserLoginLogsController::show does not exist

Means the controller does not contain show() function and Route::resource('report_user_login_logs', 'ReportUserLoginLogsController'); loads each of the available resource operations but when it gets to show() it throws an error since it doesn't exist.

You can use Partial Resource Routes to exclude a certain operations:

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

Or include only certain operations:

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