1
votes

I'm trying to refactor some code and I'm getting this error: Undefined property: Illuminate\Database\Eloquent\Builder::$total at return view('dash.dash') ->with('pending_contracts', number_format($pending->total)) Was wondering if anyone could point me in the right direction with this error. I'm sure I'm overlooking something but I can't figure out what the solution is. Here is the function for my code.

public function index()
    {
        $input = Input::all();

        $user_id = Auth::user()->id;
        $role_id = User::UserRoleData()->where('user_id', '=', $user_id)->first();
        $call_center = Auth::user()->call_center;
        $call_center = ($call_center == null ? '' : $call_center);
        $call_center = !empty($input['call_center']) ? $input['call_center'] : $call_center;
        $month = !empty($input['month']) ? $input['month'] : 'twentyfour';
        $company_type = !empty($input['company_type']) ? $input['company_type'] : 'call_center';
        $companies = Company::DistinctCompanies()->orderby('name')->get();

        $date = date('Y-m-d', strtotime('now -24 months'));

        $pending = Contract::GetData()->select(DB::raw('count(*) as total'))->where('system_status', '=', 'Pending')->where('hold', '=', '0');
        $active = Contract::GetData()->select(DB::raw('count(*) as total'))->where('system_status', '=', 'Active')->where('hold', '=', '0');
        $total_reserve = PaymentSchedules::HoldbackTotal(date("Y"))->whereNotNull('actual_payment_date')->leftjoin('contracts', 'contracts.moxy_contract_id', '=', 'payment_schedules.moxy_contract_id');
        $outstanding = ContractCancellation::GetYearToDate()->where('affected_date', '<=', date("Y-12-01"))->where('affected_date', '>=', date("Y-01-01"));
        $cancelled_contracts = Contract::GetData()->select(DB::raw('count(*) as total'))->where('system_status', '=', 'Cancelled')->where('hold', '=', '0');
        $late_contracts = Contract::GetData()->select(DB::raw('count(*) as total'))->where('system_status', '=', 'Late')->where('hold', '=', '0');
        $in_default_contracts = Contract::GetData()->select(DB::raw('count(*) as total'))->where('system_status', '=', 'In Default')->where('hold', '=', '0');
        $back_out_contracts = Contract::GetData()->select(DB::raw('count(*) as total'))->where('system_status', '=', 'Back Out')->where('hold', '=', '0');
        $pending_late = Contract::GetData()->select(DB::raw('count(*) as total'))->where('system_status', '=', 'Pending Late')->where('hold', '=', '0');
        $pending_cancellation = Contract::GetData()->select(DB::raw('count(*) as total'))->where('system_status', '=', 'Awaiting Cancellation')->where('hold', '=', '0');
        $total = Contract::GetData()->select(DB::raw('count(*) as total'));

                if($role_id['id'] == '2' && !empty($input['call_center']))
                {
                    $call_center = $input['call_center'];
                    if($call_center == '52')
                    {
                    $asi_contracts = array('26', '35', '4', '42', '45', '46', '47', '49', '50', '51');
                    $pending->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    $active->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    $total_reserve->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    $outstanding->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    $cancelled_contracts->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    $late_contracts->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    $in_default_contracts->where('contracts.call_center', '=', $call_center);
                    $back_out_contracts->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    $pending_late->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    $pending_cancellation->where('contracts.moxy_contract_id', 'not like', 'EACP%')->where('contracts.moxy_contract_id', 'not like', '9466%')->where('contracts.moxy_contract_id', 'not like', '9836%');
                    }else{
                    $pending->where('contracts.call_center', '=', $call_center);
                    $active->where('contracts.call_center', '=', $call_center);
                    $total_reserve->where('contracts.call_center', '=', $call_center);
                    $outstanding->where('contracts.call_center', '=', $call_center);
                    $cancelled_contracts->where('contracts.call_center', '=', $call_center);
                    $late_contracts->where('contracts.call_center', '=', $call_center);
                    $in_default_contracts->where('contracts.call_center', '=', $call_center);
                    $back_out_contracts->where('contracts.call_center', '=', $call_center);
                    $pending_late->where('contracts.call_center', '=', $call_center);
                    $pending_cancellation->where('contracts.call_center', '=', $call_center);
                    }
                }
        $pending->get()->first();
        $active->get()->first();
        $total_reserve->get()->first();
        $outstanding->get()->first();
        $cancelled_contracts->get()->first();
        $late_contracts->get()->first();
        $in_default_contracts->get()->first();
        $back_out_contracts->get()->first();
        $pending_late->get()->first();
        $pending_cancellation->get()->first();
        $total->get();
View::share('active_nav', 'Dashboard');
        return view('dash.dash')
            ->with('pending_contracts', number_format($pending->total))
            ->with('month', $month)->with('company_type', $company_type)
            ->with('active', number_format($active->total))
            ->with('total_reserve', money_format('$%!i', $total_reserve['holdback']))
            ->with('outstanding', $outstanding)
            ->with('year', date("Y"))
            ->with('companies', $companies)
            ->with('call_center', $call_center)
            ->with('cancelled_contracts', number_format($cancelled_contracts->total))
            ->with('late_contracts', number_format($late_contracts->total))
            ->with('in_default_contracts', number_format($in_default_contracts->total))
            ->with('back_out_contracts', number_format($back_out_contracts->total))
            ->with('pending_late', number_format($pending_late->total))
            ->with('pending_cancellation', number_format($pending_cancellation->total));
}

1
Note: You don't need to call ->get()->first(); it's redundant and potentially inefficient. Just call ->first();. Also, view(...)->with(['one' => ..., 'two' => ...]); accepts an array; you don't need multiple ->with() calls.Tim Lewis

1 Answers

0
votes

The mistake is that you don't assign the result of the query to the $pending variable but you only do the query.

// replace
$pending->get()->first();
// with
$pending = $pending->first();

The same thing applies to the other variables too.