【问题标题】:show data in blade from pivot table从数据透视表中显示刀片中的数据
【发布时间】:2021-09-22 02:17:25
【问题描述】:

我使用这样的刀片文件显示了一个名为 community_competition 的数据透视表:

但我想这样表现,怎么办?

no name push up sit up back up squat jump
1. Dr Keith Crist 20 10 12 23
2. Jaren Wunsch 34 13 24 53

这是我用来展示桌子的刀片:

<thead>
    <tr>
        <th scope="col">#</th>
        <th scope="col">name</th>
        <th scope="col">push up</th>
        <th scope="col">sit up</th>
        <th scope="col">back up</th>
        <th scope="col">squat jump</th>
    </tr>
</thead>
<tbody>
    @php 
      $no = 1; 
    @endphp
    @foreach ($data as $v)
    <tr>
        <th scope="row">{{ $no++ }}</th>
        <td>{{ $v->community->name}}</td>
        <td>{{ $v->point}}</td>
    </tr>
    @endforeach
</tbody>

CommunityCompetition模特:

public function community()
{
    return $this->belongsTo(Community::class);
}

public function competition()
{
    return $this->belongsTo(Competition::class);
}

CommunityCompetition控制器:

public function index()
{
    $data = CommunityCompetition::with(['community', 'competition'])->get();

    return view('cms.rank.index', compact('data'));
}

community表:

$table->id();
$table->string('name', 100);
id name
1 Dr.Keith Crist
2 Jaren Wunsch

competition表:

$table->id();
$table->string('name', 100);
id name
1 push up
2 sit up
3 back up
4 squat jump

community_competition表:

$table->id();
$table->bigInteger('community_id')->unsigned();
$table->bigInteger('competition_id')->unsigned();
$table->integer('point')->nullable();
$table->timestamps();

$table->foreign('community_id')->references('id')->on('communities');
$table->foreign('competition_id')->references('id')->on('competitions');
id community_id competition_id point
1 1 1 20
2 1 2 10
3 1 3 12
4 1 4 23
5 2 1 34
6 2 2 13
7 2 3 24
8 2 4 53

【问题讨论】:

    标签: laravel eloquent laravel-blade laravel-8


    【解决方案1】:

    好吧,您的表格有 6 列,而您只用此代码填充了 3 列:

    <tr>
        <th scope="row">{{ $no++ }}</th>
        <td>{{ $v->community->name}}</td>
        <td>{{ $v->point}}</td>
    </tr>
    

    因此,您需要再添加 3 个tds 来完成其他的。我不知道名称是什么,因为您的 community_competition 没有存储这 3 个缺失的列。

    你应该得到这样的结果(记住你必须使用正确的模型和属性):

    <tr>
        <th scope="row">{{ $no++ }}</th>
        <td>{{ $v->community->name}}</td>
        <td>{{ $v->point}}</td>
        <td>{{ $v->sit_up }}</td>
        <td>{{ $v->back_up }}</td>
        <td>{{ $v->squat_jump }}</td>
    </tr>
    

    【讨论】:

    • 不兄弟,积分场是根据每场比赛而定的,仰卧起坐、俯卧撑、深蹲跳都是从比赛台上获得的
    • 解释更多你的意思是什么,你已经声明了一个 6 列的表,你只填充 3 列...如果我的答案不是你想要的,那么更好地解释每列是什么,因为你不清楚
    • 哦,我明白了,您想显示一个包含这些列的表格,但是您将这些数据存储在 CommunityCompetition 但您无法引用每一个,因为每个 point 对应于sit upback up 等之一,对吧?向我们展示来自communitycompetition 的信息。
    • 是的,我已经编辑了我的问题并添加了该信息
    猜你喜欢
    • 2021-05-19
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2023-01-08
    • 2020-05-08
    相关资源
    最近更新 更多