The Laravel 4 model method failed with & ldquo; Call the undefined method & rdquo;

My models extends “BaseModel” which in its turn extends the Eloquent.

class BaseModel extends Eloquent {

public function foo($attribute)
{
    //some code
}

In my collection, where the model being instanced I’m trying to access the “foo()” method, but it responses me with “Call to undefined method”.

$data = IncomeDoc::with('details')
                            ->where('type', '!=', 2)
                            ->get();
$data = $data->foo();

Moreover, I tried to place the method “foo” in the model itself, but there was no difference.

Thanks for all

Basically get()
method returns a Collection of instances. Assume more than 1 model satisfy type != 2
condition. If you want to get first model under the condition just use first()
instead.

$data = IncomeDoc::with('details')
                        ->where('type', '!=', 2)
                        ->first();
$data = $data->foo();

Otherwise:

$collection = IncomeDoc::with('details')
                        ->where('type', '!=', 2)
                        ->get(); 

$data = [];
foreach($collection as $item) {
    $data[] = $data->foo();
}
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » The Laravel 4 model method failed with & ldquo; Call the undefined method & rdquo;

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录