Marimokung
Marimokung

Reputation: 123

Laravel : Cannot show many to many relationships in view

I cannot show many to many relationships in view It's relationships of Art objects and Exhibitions. I have many Art objects in many exhibitions table. But It doesn't show in view when I call.

In Model:

Exhibition Model:

class Exhibition extends Model
{
    protected $table = 'exhibitions';
    protected $fillable = ['Ex_id','Name','Start_date','End_date','Limit_visit','picture'];
    protected $primaryKey = 'Ex_id';

    public function ExhibitionHasUser(){
        return $this->belongsTo(ExhibitionHasUser::class,'Ex_id', 'exhibition_id');
    }
}

ExhibitionHasArt Model

class ExhibitionHasArt extends Model
{
    protected $table = 'exhibition_has_art_objs';
    protected $fillable = ['list_no','exhibition_id','art_obj_Id_no'];
    protected $primaryKey = 'list_no';

    public function Exhibition(){
        return $this->hasMany(Exhibition::class,'exhibition_id');
    }

    public function Art_obj(){
        return $this->hasMany(Art_obj::class,'Id_no');
    }
}

Art_obj Model

class Art_obj extends Model
{
    protected $table = 'art_objs';
    protected $fillable = ['Id_no','Type_of_art','Type_of_coll','Picture'];
    protected $primaryKey = 'Id_no';


    public function ExhibitionHasArt(){
        return $this->belongsTo(ExhibitionHasArt::class,'Id_no', 'art_obj_Id_no');
    }


}

In ExhibitionController

 public function show($Ex_id)
    {
        $exhibitions = Exhibition::find($Ex_id);
        $exhibitionHasArts = ExhibitionHasArt::with('Art_obj')->get();
        return view('Exhibition.ShowExhibition', compact('exhibitions','exhibitionHasArts')); 
    }

In view: I want to show the exhibition that finds from the primary key and shows art object that shows in this exhibition.

<h1>Exhibition: {{$exhibitions->Ex_id}}</h1>
            <h2>Art objects in this exhibition</h2>
            <br>
            <table class="table table-bordered table-striped"> 
                <tr>
                    <td>exhibition_id</td>
                    <td>art_obj_Id_no</td>
                    <td>Title</td>
                </tr>
                @foreach($exhibitionHasArts as $row) 
                @if($row->exhibition_id == $exhibitions->Ex_id)
                    <tr>
                        <td>{{$row->exhibition_id}}</td>
                        <td>{{$row->art_obj_Id_no}}</td>
                        <td>{{$row->Art_obj->Title}}</td>
                    </tr>
                @endif
                @endforeach 
            </table> 

But It doesn't work.

In table exhibition_has_art_objs enter image description here

Upvotes: 3

Views: 1195

Answers (2)

Emtiaz Zahid
Emtiaz Zahid

Reputation: 2835

you should change your relations like that if you really like to get the data from pivot table

please let me know if i am wrong.

Exhibition Model:

class Exhibition extends Model
{
    protected $table = 'exhibitions';
    protected $fillable = ['Ex_id','Name','Start_date','End_date','Limit_visit','picture'];
    protected $primaryKey = 'Ex_id';

    public function ExhibitionHasUser(){
        return $this->hasMany(ExhibitionHasUser::class,'exhibition_id','Ex_id');
    }
}

ExhibitionHasArt Model

class ExhibitionHasArt extends Model
{
    protected $table = 'exhibition_has_art_objs';
    protected $fillable = ['list_no','exhibition_id','art_obj_Id_no'];
    protected $primaryKey = 'list_no';

    public function Exhibition(){
        return $this->belongsTo(Exhibition::class,'exhibition_id', 'Ex_id');
    }

    public function Art_obj(){
        return $this->belongsTo(Art_obj::class,'art_obj_Id_no', 'Id_no');
    }
}

Art_obj Model

class Art_obj extends Model
{
    protected $table = 'art_objs';
    protected $fillable = ['Id_no','Type_of_art','Type_of_coll','Picture'];
    protected $primaryKey = 'Id_no';


    public function ExhibitionHasArt(){
        return $this->hasMany(ExhibitionHasArt::class,'art_obj_Id_no','Id_no');
    }
}

Upvotes: 1

Mou Hsiao
Mou Hsiao

Reputation: 366

In your case, there's no need to define ExhibitionHasArt. From laravel many to many documentation.

What I tried here is modifying your codes directly, only for expressing the logic of many-to-many, so you may have to debug them and to fit your database yourself.

In Models:

class Exhibition extends Model
{
    protected $table = 'exhibitions';
    //While the primary key of a table is set auto increasing, it doesn't need to be fillable.
    protected $fillable = [/*'Ex_id'*/,'Name','Start_date','End_date','Limit_visit','picture'];
    protected $primaryKey = 'Ex_id';

    public function art_objs() {
        //use "belongsToMany" instead of "belongsTo".
        return $this->belongsToMany(Art_obj::class, 'exhibition_has_art_objs', 'exhibition_id', 'art_obj_id_no');
    }
}

class Art_obj extends Model
{
    protected $table = 'art_objs';
    //The primary key of a table is usually set auto increasing, no need to be fillable.
    protected $fillable = [/*Id_no, */'Type_of_art','Type_of_coll','Picture']; 
    protected $primaryKey = 'Id_no';

    public function exhibitions() {
        return $this->belongsToMany(Exhibition::class,'exhibition_has_art_objs', 'art_obj_id_no', 'exhibition_id');
    }
}

In ExhibitionController:

public function show($Ex_id)
{
    $exhibition = Exhibition::find($Ex_id);
    return view('Exhibition.ShowExhibition', compact('exhibition')); 
}

In View:

<h1>Exhibition: {{$exhibition->Ex_id}}</h1>
<h2>Art objects in this exhibition</h2>
<br>
<table class="table table-bordered table-striped"> 
    <tr>
        <td>exhibition_id</td>
        <td>art_obj_Id_no</td>
        <td>Title</td>
    </tr>
    @foreach($exhibition->art_objs as $art_obj) 
        <tr>
            <td>{{$exhibition->Ex_id}}</td>
            <td>{{$art_obj->Id_no}}</td>
            <td>{{$art_obj->Type_of_art}}</td>
        </tr>
    @endforeach 
</table>

Upvotes: 0

Related Questions