Home > Software engineering >  Laravel data loading using many to many relationhips
Laravel data loading using many to many relationhips

Time:11-27

Trying to get data using many-to-many relationships in laravel. My files and codes are described as below.

Models:

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

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')->first();
  // 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, id(grades) & grade" 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>
      <td class="text-left">{{ $specification->grades->grade }}</td>
      <td><a href="/specifications/{{ $specification->id }}/edit">Edit</a></td>
    </tr>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  </tbody>
@empty
  <p><strong>No data to preview</strong></p>
@endforelse

At last I ended up with error below.

ErrorException Trying to get property 'id' of non-object

I've tried many ways to get loose from this confusion. I want to know that I am on the right approach, any helps will be appreciated to identify this error.

CodePudding user response:

in Specification Model public function grades(){ return $this->belongsToMany(Grade::class,'grades','id')->withTimestamps();}

CodePudding user response:

Got the problem according to your reply of my comment. In your controller, you are using ->first() rather you should use ->get() to get a collection of specifications and then foreach/forelse that collection in the blade.

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