【问题标题】:Laravel vue array in array issue数组问题中的Laravel vue数组
【发布时间】:2019-02-12 07:36:46
【问题描述】:

我正在尝试从 2 个数据库表中获取数据并在我的组件中的 1 个表体中返回所有数据。

代码

controller

public function show($id)
    {
        $history = Payment::where('account_id', $id)->with('account')->orderby('id', 'desc')->get();
        $balance = Account::where('id' , $id)->select('balance')->first();
        return response()->json([
            $history,$balance
        ]);
    }

component

<table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th class="text-center">#</th>
                    <th class="text-center">Date</th>
                    <th class="text-center">Amount</th>
                    <th class="text-center">Note</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(history,index) in histories" @key="index">
                    <td width="50" class="text-center">{{index+1}}</td>
                    <td class="text-center" width="100">
                        {{history.created_at}}
                    </td>
                    <td class="text-center" width="300">Rp. {{ formatPrice(history.balance) }}</td>
                    <td class="text-center">{{history.note}}</td>
                </tr>
            </tbody>
</table>

export default {
        data() {
            return {
                histories : []
            }
        },
        beforeMount(){
            let user_id = this.user.id;
            axios.get('/api/account/'+user_id).then(response => this.histories = response.data)
        },
// rest of it
</script>

在上图中

我从历史数组中的Account 表中获取我的余额,而我从历史数组中的Payment 表中获取历史

我想要的是取出 histories 数组并将其加入 histories 就在有余额数据的地方。 所以以后我可以有类似的东西:

histories: array[3]
0:...
1:...
2:...

我该怎么做?

更新

我在控制器中进行了更改,现在数据结果变成了我想要的(全部在一个数组中),但不知何故它没有返回所有数据。

代码

public function show($id)
    {
        // $history = Payment::where('account_id', $id)->with('account')->orderby('id', 'desc')->get();
        // $balance = Account::where('id' , $id)->select('balance')->first();
        $history = DB::table('payments')
        ->where('account_id', $id)
        ->join('accounts', 'accounts.id', '=', 'payments.account_id')
        ->get();
        return response()->json($history, 200);
    }

它应该是 3,但它只返回 2。

【问题讨论】:

    标签: arrays laravel vue.js vuejs2 vue-component


    【解决方案1】:

    已解决

    我使用了 push 方法,现在它可以按我的意愿工作了。这是有需要的人的最后代码。

    public function show($id)
        {
            $his = Payment::where('account_id', $id)->with('account')->orderby('id', 'desc')->get();
            $balance = Account::where('id' , $id)->select('balance')->first();
            $history = $his->push($balance);
            return response()->json($history, 200);
        }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 1970-01-01
      • 2018-12-08
      • 2017-11-12
      • 2020-07-27
      • 2017-11-14
      • 2020-11-24
      • 2020-11-24
      • 1970-01-01
      相关资源
      最近更新 更多