我有一张table叫 journal_details
它有一列名为 transaction_by_to
. 我在存钱 account_head_id
值为 json_encode
格式 transaction_by_to
在表中 journal_details
. 现在我想运行一个select查询,从中获取帐户头名称 account_heads
哪里 transaction_by_to
json格式的id将从 account_heads
. 在控制器中,我尝试了类似于bellow的东西,但只得到一个。但我想展示一下 account_heads
对于所有json格式的ID。
public function ledgerSubmit(Request $request)
{
$acId = $request->acount_head;
$startDate = Carbon::parse($request->start_date)->format('Y-m-d');
$endDate = Carbon::parse($request->end_date)->format('Y-m-d');
$reportType = $request->report_type;
$nameOfAccount = AccountHead::find($acId);
$ledgers = DB::table('journal_details')
->join('journals', 'journal_details.journal_id', '=', 'journals.id')
->join('account_heads', 'journal_details.account_head_id', '=', 'account_heads.id')
->where('journal_details.account_head_id', $acId)
->select('journals.number as journalNo', 'journals.journal_date as journalDate', 'journal_details.*', 'account_heads.name as nameOfAccount')
->get();
if (count($ledgers)) {
$transactionByToId = $ledgers[0]->transaction_by_to;
foreach (json_decode($transactionByToId) as $tId) {
$transactionByTo = AccountHead::where('id', $tId)->get();
}
}
return view('report.ledger.searched-result', compact('reportType', 'startDate', 'endDate', 'ledgers', 'transactionByTo', 'nameOfAccount'));
}
在刀片视图中-
@foreach($ledgers as $ledger)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $ledger->journalNo }}</td>
<td>{{ date('jS F, Y', strtotime($ledger->journalDate)) }}</td>
<td>{{ $ledger->nameOfAccount }}</td>
<td>
{{ $transactionByTo[0]->name }}
</td>
<td>
{{ number_format($ledger->debit, 2,".",",") }}
</td>
<td>
{{ number_format($ledger->credit, 2,".",",") }}
</td>
</tr>
@endforeach
``` `["16","17","7","11"]` 是 `transaction_by_to` json格式的列值 `journal_details` 这些身份证是 `id` 为了 `account_heads` table。
1条答案
按热度按时间mwkjh3gx1#
您的代码中有几个错误,如以下代码:
它将始终保持单个值,即最后一个值。所以看起来只有一个记录。
但是你为什么不一次做以下操作来获取所有记录:
其次,我没有看到您正在处理accounthead变量(根据我的代码示例,
$accountHeads
)在不属于的视图文件中$leders
变量。如果你想把它压下去的话$ledgers
变量,然后你应该存储在它下面。注意:我还没有测试上面的代码,但是概念是相似的。