Laravel:无法显示多对多的关系

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel:无法显示多对多的关系相关的知识,希望对你有一定的参考价值。

我无法展示多对多的关系它是艺术品和展览的关系。我在很多展览桌上都有很多艺术品。但是当我打电话时,它并没有显示出来。

在模型中:

展览模型:

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

展览艺术模型

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模型

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


}

在ExhibitionController中

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

在视图中:我想展示从主键找到的展览,并展示在本次展览中展示的艺术品。

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

但它不起作用。

在表展_has_art_objs enter image description here

答案

在您的情况下,没有必要定义ExhibitionHasArt。 From laravel many to many documentation

我在这里尝试的是直接修改你的代码,只是为了表达多对多的逻辑,所以你可能需要自己调试它们并适合你的数据库。

在模型中:

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

在ExhibitionController中:

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

在视图中:

<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>
另一答案

如果你真的想从数据透视表中获取数据,你应该改变你的关系

如果我错了,请告诉我。

展览模型:

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

展览艺术模型

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模型

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

以上是关于Laravel:无法显示多对多的关系的主要内容,如果未能解决你的问题,请参考以下文章

如何在Laravel中的多对多关系中添加“别名”?

Laravel一张表连接到另外两个由多对多的关系

Laravel中多对多关系的多重搜索子句

Laravel 上的多对多问题关系

Laravel 多对多关系计数

Laravel5.1 模型 --多对多关系