doumi5223 2017-07-19 13:27 采纳率: 100%
浏览 155
已采纳

在Laravel中将模型中的函数调用到控制器中

Is it possible to call function from model into the controller in foreach loop?

This is my model

class Order extends Eloquent
{
    public static function getData($data)
    {
        // function code 
    }
}

This is what I'm trying in my controller

$get = Order::all();
foreach ($get->getData($get->details) as $itemId => $details) {
    dd($details);
}

This produce error

Error: Call to undefined method Illuminate\Database\Eloquent\Collection::getData()

I've tried exact same code in my blade and there is working without any issue.

So, how I'm suppose to call and use the function in my controller?

  • 写回答

1条回答 默认 最新

  • dongtuoleng8624 2017-07-19 13:30
    关注

    The getData() function is declared as static in your class

    public static function getData($data)
    {
        // function code 
    }
    

    So it should be called using class name as Order::getData($get->details)

    foreach (Order::getData($get->details) as $itemId => $details) {
        dd($details);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?