duandai0373 2018-12-03 20:27
浏览 77

Laravel控制器中的自定义计算

I am new to Laravel. I'm building a small app which displays the user table from the DB, but before sending it to the view, I want to include a custom value from another function.

The accounts table fetches the list of accounts from MySQL, and I want to include a custom function called getStatus() which I get from an API.

Code

<?php

public function accounts()
{
    $accounts = DB::table('accounts')
        ->where('profile_id', '=', Auth::id())
        ->get();

    foreach ($accounts as $account) {

        $account->status = getStatus($account->accno);
    }

    $data = compact('accounts');

    return view::make('transactions', ['accounts' => $data]);
}

View

@foreach ($accounts as $account)
    <tr>
        <td></td>
        <td>{{ $account->login }}</td>
        <td>{{ $account->status }}</td>
    </tr>
@endforeach
  • 写回答

2条回答 默认 最新

  • dougou6213 2018-12-03 20:42
    关注

    You can do it like this.

    $accounts = $accounts->map(function($account){
       $account->status = getStatus($account->accno)
    });
    

    Hope this will help you.

    Thanks

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效