Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在laravel中显示来自多个表和外键的数据_Laravel_Foreign Keys_Relationship_Laravel Blade - Fatal编程技术网

在laravel中显示来自多个表和外键的数据

在laravel中显示来自多个表和外键的数据,laravel,foreign-keys,relationship,laravel-blade,Laravel,Foreign Keys,Relationship,Laravel Blade,我有两张桌子: bonhome +----+--------------+ | id | name | +----+--------------+ | 1 | Alec Badwin | | 2 | Maria Quiban | +----+--------------+ +----+---------------+ | id | name | +----+---------------+ | 1 | 30 Rock | | 2 | Go

我有两张桌子:

bonhome

+----+--------------+
| id |     name     |
+----+--------------+
|  1 | Alec Badwin  |
|  2 | Maria Quiban |
+----+--------------+
+----+---------------+
| id |     name      |
+----+---------------+
|  1 | 30 Rock       |
|  2 | Good Day L.A. |
+----+---------------+
系列

+----+--------------+
| id |     name     |
+----+--------------+
|  1 | Alec Badwin  |
|  2 | Maria Quiban |
+----+--------------+
+----+---------------+
| id |     name      |
+----+---------------+
|  1 | 30 Rock       |
|  2 | Good Day L.A. |
+----+---------------+
我还有一个名字叫:意甲人事部

+----+-------------+----------+--------------+
| id | bonhomme_id | serie_id |  personnage  |
+----+-------------+----------+--------------+
|  1 |           1 |        1 | Jack Donaghy |
|  2 |           2 |        2 | Maria Quiban |
+----+-------------+----------+--------------+
我想显示serie_personnage中的personnage,并在我的刀片中将其与关联的bonhomme(在我的表bonhomme中)的名称链接

换句话说,我希望在我的blade中有一个表,其中第1列是bonhomme的名称,第2列是关联的personnage

我如何处理拉威尔的人际关系?我试过了,但效果不好

控制器

公共功能显示($id)
{
$serie=serie::where('id',$id)->first();
返回视图('admin.series.casts.show')->with serie($serie);
}
模型系列

public function personnages()
{
返回$this->hasMany('App\Models\Admin\SeriePersonnage');
}
公职人员()
{
返回$this->belongtomany('App\Models\Admin\bonhome','serie\u personnage','serie\u id','bonhome\u id');
}
刀片


@foreach($serie->acteurs as$acteur)
@foreach($serie->personnages as$personnage)
{{$acteur->nom}
{{$personnage->personnage}
{{$personnage->rang}
@endforeach
@endforeach

作为法国开发者,我也必须帮助你!首先,我想用一种更简单的方法来改变你的控制器以获得意甲:

公共功能显示($id)
{
$serie=serie::findOrFail($id);
返回视图('admin.series.casts.show')->with serie($serie);
}
编辑:我使用
Model::findOrFail($id)illumb\Database\Eloquent\ModelNotFoundException
。如果您没有捕获到异常,Laravel将自动发送404HTTP响应。它可以防止用户使用过时链接时出错

那么我认为使用
foreach
循环两次不是一个好主意。。。我宁愿在personnages上只使用一个
foreach
循环。然后,我将从Personnage模型中获取角色的演员。它应该类似于下面的代码:

App\Models\Admin\SeriePersonnage.php


您是否尝试使用您的
bonhome
serie
表之间的关系?据我所见,
serie\u personnage
作为一个数据透视表工作,因此您可以这样设置它并从
serie
检索数据您是否为您的模型serie\u personnage设置了任何关系?我已经尝试了多对多关系,但我不确定是否正确。。。模型中的正确语法是什么?我如何在刀片中检索数据?嗨,Alexandre,非常感谢你的回答和所有提示!我在拉威尔和德夫都是新手,所以有人给我小费总是件好事!我尝试了你的解决方案,但是当我到达{{personnage->actor->nom}}时,出现了一个“试图获取非对象的属性'nom'的”错误:(你在personnage的模型上添加了actor()方法了吗?是的,我把公共函数actor(){return$this->belongsTo(bonhome::class);}在我的SeriePersonnage模型中,我真的不知道我做错了什么,这让我发疯了!试着用
dd($personnage)
看看你的
$personnage
对象里面有什么,让我们看看你得到了什么。我可能写了一些错误的SeriePersonnage{674▼ #表:“serie#u personnage”#connection:“mysql”#primaryKey:“id”#keyType:“int”+递增:true#with:[]#withCount:[]#perPage:15+存在:true+最近创建:false#属性:数组:5[▼ “id”=>1“bonhomme_id”=>50“serie_id”=>2“personnage”=>1]#原始:数组:5[▶] #更改:[]强制转换:[]可观察对象:[]关系:[]受保护:数组:1[▶] }