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

如何根据Project表ID筛选展示对应Collabs表的关联数据?

解决方法

要实现仅展示当前项目的Collabs数据,需要从后端数据筛选和前端代码优化两方面调整:

1. 后端过滤指定项目的Collabs数据

假设你用Laravel框架(从Blade语法判断),通过以下两种方式获取目标数据:

方式一:利用Eloquent关联查询

先在Project模型中定义与Collab的关联关系:

// app/Models/Project.php
public function collabs()
{
    return $this->hasMany(Collab::class, 'id_project');
}

然后在控制器中,根据当前项目ID(比如从路由参数获取)关联查询:

// 假设路由为 /projects/{project}/collabs
$project = Project::findOrFail($request->route('project'));
$istoric = $project->collabs; // 仅获取该项目对应的Collabs数据

return view('你的视图文件名', compact('istoric'));

方式二:直接条件查询

如果不需要关联模型,直接通过where条件筛选:

$projectId = 10; // 替换为实际的当前项目ID(可从路由/请求中获取)
$istoric = Collab::where('id_project', $projectId)->get();

return view('你的视图文件名', compact('istoric'));

2. 优化前端Blade代码

原代码会为每条Collab记录生成独立表格,布局不合理,调整后将表头移出循环,空状态时统一提示:

<div class="mb-3">
    <table class='table'>
        <tr class="table-row-heads">
            <th>Id Proiect</th>
            <th>Tip actiune </th>
            <th>Colaborator </th>
            <th>Suma </th>
            <th>Data </th>
        </tr>
        @forelse ($istoric as $istProj)
            <tr class="table-row-data">
                <td>{{ $istProj->id_proiect }}</td>
                <td>{{ $istProj->action_type }}</td>
                <td>{{ $istProj->colaborator_id }}</td>
                <td>{{ $istProj->suma }}</td>
                <td>{{ $istProj->data }}</td>
            </tr>
        @empty
            <tr>
                <td colspan="5" class="text-center">
                    <h2>Nu au fost gasite inregistrari</h2>
                </td>
            </tr>
        @endforelse
    </table>
</div>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 11:50:22