Laravel,模型的范围方法

时间:2015-12-17 00:29:55

标签: php laravel

我写了一个计算月收入的Laravel应用程序。

收入表有一个保存每月价值的列。在某些月份,该值将为0,但是当我使用以下方法访问该方法时

$服务 - >创收> getMonthTotal(11);

我收到以下错误(public function scopeGetMonthTotal($query, $month) { $result = $query->where('month',$month)->sum('value'); return $result; }

这是我使用的范围方法,其中$ result返回0,laravel给我错误。

关于如何为回显值返回0的任何想法?

    // Get the file version for the notepad.
    FileVersionInfo myFileVersionInfo =
        FileVersionInfo.GetVersionInfo(Environment.SystemDirectory + "\\Notepad.exe");

    // Print the product major part number.
    textBox1.Text = "Product major part number: " + myFileVersionInfo.ProductMajorPart;

1 个答案:

答案 0 :(得分:1)

作为属性访问关系将自动运行get(),从而导致您的错误。您需要以方法的形式访问您的关系:

$service->income()->getMonthTotal(11);

要查看结果,请务必自己运行get()

echo $service->income()->getMonthTotal(11)->get();