作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法在 View 中显示多对多关系这是艺术品和展览的关系。我在许多展览 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->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');
}
}
在展览 Controller 中
public function show($Ex_id)
{
$exhibitions = Exhibition::find($Ex_id);
$exhibitionHasArts = ExhibitionHasArt::with('Art_obj')->get();
return view('Exhibition.ShowExhibition', compact('exhibitions','exhibitionHasArts'));
}
在 View 中:我想展示从主键找到的展览并展示在这个展览中展示的艺术品。
<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>
但它不起作用。
最佳答案
如果你真的想从数据透视表中获取数据,你应该像那样改变你的关系
please let me know if i am wrong.
展览模型:
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');
}
}
关于php - 拉维尔 : Cannot show many to many relationships in view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53201587/
我正在使用 git clone 部署我的 Laravel 项目并使用 git pull 进行更新 它工作正常,但每次部署时,我都必须从 config/app.php providers 数组和 ali
我是一名优秀的程序员,十分优秀!