【问题标题】:Undefined property: Illuminate\Database\Eloquent\Builder::$total未定义属性:Illuminate\Database\Eloquent\Builder::$total
【发布时间】:2019-08-26 14:10:12
【问题描述】:

我正在尝试重构一些代码,但出现此错误:未定义属性:Illuminate\Database\Eloquent\Builder::$total at 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));
}

【问题讨论】:

  • 注意:您不需要调用-&gt;get()-&gt;first();,它是多余的并且可能效率低下。只需致电-&gt;first();。此外,view(...)-&gt;with(['one' =&gt; ..., 'two' =&gt; ...]); 接受一个数组;您不需要多次-&gt;with() 调用。

标签: php laravel


【解决方案1】:

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

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

同样的事情也适用于其他变量。

【讨论】:

  • 使用该解决方案它给我带来了这个错误:类 Illuminate\Database\Eloquent\Builder 的对象无法转换为字符串
  • 我很确定那是来自另一个地方。检查堆栈跟踪并查看抛出的位置
猜你喜欢
  • 2016-09-05
  • 2016-11-27
  • 2018-09-29
  • 2016-06-02
  • 1970-01-01
  • 2018-11-22
  • 2018-11-19
  • 2020-01-23
  • 2020-07-08
相关资源
最近更新 更多