【问题标题】:Multiple variable iteration within foreach loop in laravel viewlaravel视图中foreach循环内的多变量迭代
【发布时间】:2022-01-05 00:55:21
【问题描述】:

我通过 laravel 中的多对多关系从不同的表中获取数据。我的模型表如下所示。

等级.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Grade extends Model{
    protected $guarded = [];
    public function specifications(){
        return $this->belongsToMany(Specification::class)->withTimestamps();
    }
    public function gnames(){
        return $this->belongsToMany(Gname::class)->withTimestamps();
    }
    public function gsizes(){
        return $this->belongsToMany(Gsize::class)->withTimestamps();
    }
}

Gsize.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Gsize extends Model{
    protected $guarded = [];

    public function grades(){
        return $this->belongsToMany(Grade::Class);
    }

    public function specifications(){
        return $this->belongsToMany(Specification::class);
    }
}

Gname.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Gname extends Model{
    protected $guarded = [];

    public function grades(){
        return $this->belongsToMany(Grade::Class);
    }

    public function specifications(){
        return $this->belongsToMany(Specification::Class);
    }
}

规范.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Specification extends Model{
    protected $guarded = [];

    public function grades(){
        return $this->belongsToMany(Grade::class)->withTimestamps();
    }
    public function gnames(){
        return $this->belongsToMany(Gname::Class)->withTimestamps();
    }
    public function gsizes(){
        return $this->belongsToMany(Gsize::class)->withTimestamps();
    }
}

我在SpecificaitonController中的索引方法是这样的,

public function index(Specification $specification){        
  $specifications = Specification::with('grades:id,grade')->get();
  // dd($specifications);
  return view('/home.specification.index', compact('specifications'));
}

当我 dd($specificaitons);输出将是,

我的目的是通过“规格和等级”模型中的多对多关系从规格、等级、表格中显示“id(规格)、规格_编号、等级、gname和gsize。视图如下所示。

@forelse ($specifications as $specification)
 <tbody>
  <tr>
   <td class="text-left">{{ $specification->id }}</a></td>
   <td class="text-left">{{ $specification->specification_no}/td>
   @foreach ($specification as $gradeNames => $grade)
     <td class="text-left">grade-gname-gsize</td>
   @endforeach
   <td><a href="/specifications/{{ $specification->id}}/edit">Edit</a></td>
    </tr>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
   </tbody>
  @empty
<p><strong>No data to preview</strong></p>
@endforelse

我正在尝试在表格的单个单元格中显示等级、gname 和 gsize,例如 grade-gname-gsize

我尝试了不同的方法来解决这个问题。任何答案将不胜感激,以接近我的目标。

【问题讨论】:

  • 创建一个Accessors?
  • 您想从Specification 关系或Grade 关系中获取gnamesgsizes 还是grades 表上的字段?
  • @Rwd : Grades, gsizes & gnames 与 Specification 表有单独的多对多关系。 Grades 表也有多对多的 g​​names 和 gsizes。保存规范时,它也会保存等级、gnames 和 gsizes 关系。我从 grades 表中获取 grade、从 gnames 表和 gsize中获取 gname 的字段> 来自与规格相关的 gsizes 表。

标签: arrays laravel foreach


【解决方案1】:

我尝试过这样的事情,它对我来说接近了我的期望。但是不知道是好是坏。但它工作正常。

@forelse ($specifications as $specification)
  <tr>
   <td class="text-left">{{ $specification->id }}</a></td>
   <td class="text-left">{{ $specification->specification_no }}</td>
   @foreach ($specification->grades as $grade)
     <td class="text-left">
       {{ $grade->grade }}
       -
      @foreach ($specification->gnames as $gname)
        {{ $gname->gname }}
      @endforeach
        -
      @foreach ($specification->gsizes as $gsize)
        {{ $gsize->gsize }}
      @endforeach
     </td>
   @endforeach
  <td><a href="/specifications/{{ $specification->id }}/edit">Edit</a></td>
 </tr>
 @empty
   <p><strong>No data to preview</strong></p>
 @endforelse

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2018-11-18
    • 2017-08-27
    • 1970-01-01
    • 2017-01-15
    • 2018-08-05
    • 2015-03-09
    相关资源
    最近更新 更多