MD40
MD40

Reputation: 325

Multiple variable iteration within foreach loop in laravel view

I am grabbing data through the many-to-many relationship in laravel from separate tables. My model tables are like below.

Grade.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);
    }
}

Specification.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();
    }
}

My index method in SpecificaitonController is like this,

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

When I dd($specificaitons); output will be,

enter image description here

My purpose is to display "id(specification), specification_no, grade,gname & gsize from the specifications,grades, table through the many to many relationships in the "Specification & Grade" models. The View is like below.

@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

I am trying to display grade,gname & gsize like grade-gname-gsize within on single cell in the table.

I've tried different ways to approach this. Any answer will be appreciated to approach my target.

Upvotes: 0

Views: 58

Answers (1)

MD40
MD40

Reputation: 325

I tried something like this and it works for me to approach my expectation. But I don't know whether it is good or bad. But it works fine.

@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

Upvotes: 1

Related Questions