noblemfd
1 week ago
193
12
Laravel

Laravel - production.ERROR: Undefined variable: data

Posted 1 week ago by noblemfd

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 user_logs($id = "")
{
    $userCompany = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;
    $userId = Auth::user()->id;
    $employeecode = Auth::user()->employee_code;
    
    
    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();
    }        

    $chart_settings = [
        'chart_title'        => 'Users By Months',
        'chart_type'         => 'line',
        'report_type'        => 'group_by_date',
        'model'              => 'App\User',
        'group_by_field'     => 'last_login_at',
        'group_by_period'    => 'month',
        'aggregate_function' => 'count',
        'filter_field'       => 'last_login_at',
        'column_class'       => 'col-md-12',
        'entries_number'     => '5',
    ];
    $chart = new LaravelChart($chart_settings);

    return view('report.report_user_login_logs.user_logs', compact( 'chart'))
            ->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.user_logs')}}/"
            }
            else{
                var url = "{{route('report.report_user_login_logs.user_logs')}}/" + $(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::get('report_user_login_logs/user_logs/{id?}', 'ReportUserLoginLogsControlle[email protected]_logs')->name('report_user_login_logs.user_logs');

   });

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>

When I select "All", it works.

But when I select

     "logged in users"   as in  "logged_in" => "logged in users",

or "not logged in users" "not_logged_in" => "not logged in users",

I got this error:

production.ERROR: Undefined variable: data

How do I resolve it?

Thank you

Please sign in or create an account to participate in this conversation.