0

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.

3
  • Create an Accessors? Commented Nov 27, 2021 at 9:02
  • Are you wanting to get the gnames and gsizes from the Specification relationship or the Grade relationship....or are these also fields on the grades table? Commented Nov 27, 2021 at 12:08
  • @Rwd : grades, gsizes & gnames have separate many-to-many relationships with Specification table. Also Grades table have many-to-many with gnames & gsizes separately. When saving the specification, it will save grades, gnames & gsizes relationships too. Field that I am fetching grade from grades table, gname from gnames table & gsize from gsizes table which are related to the specifications. Commented Nov 27, 2021 at 15:34

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.