You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel开发:如何在Blade视图表格中展示Controller foreach处理后的自定义字段数据

Hey there! Let's get this sorted out quickly—you're almost there, just a small misalignment between the data you're processing and what you're passing to the view.

The Core Issue

Right now, your Blade template is looping over $videohistories, which is a Laravel Query Builder instance, not the collection you added the total_ticket field to. When Blade loops over a Query Builder, it runs a fresh get() call under the hood—this new query doesn't include the custom field you manually added to $videohistoriesss.

Step-by-Step Fix

1. Update Your Controller to Pass the Right Data

First, make sure you're selecting all the fields your view needs (not just id and prop_table), then pass the processed collection to your view instead of the raw Query Builder:

$videohistoriesQuery = VideoHistory::where('user_id', $id)
    ->when(!empty($begin_date), function ($query) use ($int_begin_date) {
        $query->where('begin_time', '>=', $int_begin_date);
    })
    ->when(!empty($int_end_date), function ($query) use ($int_end_date) {
        $query->where('begin_time', '<=', $int_end_date);
    })
    ->orderBy('id', 'desc');

// Select ALL fields needed for the view: create_time, end_time, begin_time, plus id/prop_table
$processedVideoHistories = $videohistoriesQuery->get(['id', 'prop_table', 'create_time', 'end_time', 'begin_time']);

// Add the total_ticket field to each item (using collection's each() is cleaner than foreach)
$processedVideoHistories->each(function ($item) use ($id) {
    $item->total_ticket = DB::table($item->prop_table)
        ->where('to_user_id', $id)
        ->where('video_id', $item->id)
        ->sum('total_ticket');
});

// Pass the processed collection to your view
return view('your-view-path', compact('processedVideoHistories'));

2. Adjust Your Blade Template to Use the Processed Data

Now loop over the collection you passed, and directly access the total_ticket field in the fourth column:

@foreach ($processedVideoHistories as $videohistory)
    <tr>
        <td>{{ date("Y-m-d H:i:s", $videohistory->create_time) }}</td>
        <td>{{ date("Y-m-d H:i:s", $videohistory->end_time) }}</td>
        <td>{{ diff_date_format($videohistory->begin_time, $videohistory->end_time, "%H sa %i dk. %s sn") }}</td>
        <td>{{ $videohistory->total_ticket ?? 0 }}</td> <!-- Use ?? 0 to handle empty sums gracefully -->
    </tr>
@endforeach

Bonus: Clean Up with a Model Accessor

If you need this total_ticket value in multiple places, you can move the logic to your VideoHistory model using an accessor—this keeps your controller lean:

// In app/Models/VideoHistory.php
public function getTotalTicketAttribute()
{
    return DB::table($this->prop_table)
        ->where('to_user_id', $this->user_id)
        ->where('video_id', $this->id)
        ->sum('total_ticket');
}

Then in your controller, you just need to ensure prop_table and user_id are selected, and you can skip the loop entirely. The accessor will auto-calculate the value when you call $videohistory->total_ticket in Blade.

内容的提问来源于stack exchange,提问作者ibrahimozden

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 23:33:13