未定义的属性:Illuminate \ Database \ Eloquent \ Builder :: $ total

时间:2019-08-26 14:10:12

标签: php laravel

我正在尝试重构一些代码,但出现此错误:未定义的属性:Illuminate \ Database \ Eloquent \ Builder :: $ total  return view('dash.dash') ->with('pending_contracts', number_format($pending->total)) 想知道是否有人可以通过此错误指向我正确的方向。我确定我忽略了某些事情,但我不知道解决方案是什么。这是我的代码的功能。

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 个答案:

答案 0 :(得分:0)

错误是您没有将查询结果分配给$pending变量,而只执行了查询。

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

其他变量也一样。