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,
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.

gnamesandgsizesfrom theSpecificationrelationship or theGraderelationship....or are these also fields on thegradestable?