【发布时间】:2019-01-08 05:11:26
【问题描述】:
我有一个名为journal_details 的表,它有一个名为transaction_by_to 的列。我将account_head_id 值保存为json_encode 格式的transaction_by_to 在表journal_details 中。现在我想运行一个选择查询以从account_heads 获取帐户负责人名称,其中 JSON 格式的transaction_by_to id 将从account_heads 中找到 id。在控制器中,我尝试了类似下面的方法,但它只有一个。但我想显示来自 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_tojournal_details 中json 格式的列值,这些ID 是id 用于account_heads 表。
【问题讨论】:
标签: php mysql arrays json laravel