Laravel - 属性 [文件] 在此 collection 实例上不存在
Laravel - Property [filieres] does not exist on this collection instance
我的学校应用程序中有一个小应用程序。我想展示一所学校的所有院系。事实上,在我的数据库中,一所学校可以有一个或多个学院,一个学院可以属于一个或多个学校。
模范学校:
public function filieres ()
{
return $this->belongsToMany('App\Models\Filiere','etablissement_filieres','id_etablissements','id_filieres');
}
public function etablissement_filieres(){
return $this->hasMany('App\Models\Etablissement_filiere');
}
protected $fillable = [
'nom', 'image', 'ville', 'adresse', 'contact_1', 'contact_2',
'email', 'logo', 'presentation', 'brochure', 'localisation',
];
型号table枢轴Etablissement_filiere
:
public function filiere(){
return $this->belongsTo('App\Models\Filiere');
}
protected $fillable = [
'id', 'id_etablissements', 'id_filieres', 'prise_en_charge', 'prix',
];
模型文件:
public function etablissements ()
{
return $this->belongsToMany(Etablissement::class,'etablissement_filiere');
}
protected $fillable = [
'nom', 'diplome_requis', 'diplome_obtenu', 'prix', 'duree', 'type',
];
控制器:
public function show($id)
{
$faculty = Etablissement_filiere::where('id_etablissements','=','$id')->get();
return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissements'));
}
Blade 查看:
@foreach($faculty->filieres as $filiere)
<div class="container bg-fil py-4 my-5">
<div class="row pl-5">
<div class="col-md-9">
<h6 class="font-weight-bold">{{ $filiere ->nom}} <br>
<span class="text-primary"> {{ $filiere ->diplome_obtenu}}</span></h6>
</div>
<div class="col-md-3 pt-n5">
<img src="{{asset($etablissement->image)}}" alt="">
</div>
</div>
<div class="row pl-5 mt-md-n5">
<div class="col-md-6">
<h6> <strong> Diplôme réquis</strong>: {{ $filiere ->diplome_requis}} <br>
<strong>Durée</strong>: {{ $filiere ->duree}} <br>
<strong>Montant de la formation</strong>: {{ $etablissement_filieres ->prix}}</h6>
</div>
<div class="col-md-6">
<h6> <strong> Mode d'etude</strong>: {{ $filiere ->type}} <br>
<strong>Prise en charge</strong>: {{ $etablissement_filieres ->prise_en_charge}}</h6>
</div>
</div>
<div class="row pl-5 mt-4">
<div class="col-md-6">
<a href="{{ route('inscription') }}" class="btn btn-success font-weight-bold w-75 now">INSCRIVEZ VOUS MAINTENANT</a>
</div>
</div>
</div>
@endforeach
我正在尝试显示学校的所有院系,但出现此错误:
Property [filieres] does not exist on this collection instance.
你能告诉我防止错误的地方吗?
将代码更改为
public function show($id)
{
$faculty = Etablissement_filiere::where('id_etablissements','=',$id)->first();
return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissements'));
}
您的直接视图错误是当它试图计算 $faculty->filieres
.
时
调用类似 Model::where()->get()
return 模型的集合实例(类似于可迭代组),而不是单个模型。即使该集合只包含一个模型,它仍然是一组而不是模型本身。
因此,$faculty = Etablissement_filiere::where('id_etablissements','=','$id')->get();
return 是一个 集合 ,而不是单个模型。单个模型可能有 filieres
属性,但 Collection 实例(它本身是 Laravel 中的 class)没有。所以,如果你想要的只是第一个结果,而不是一个集合,那么以 ->first()
而不是 ->get()
.
结束该行
然而,即使你解决了这个问题,你还有第二个问题。您正在获取数据透视模型 Etablissement_filiere
的实例,然后尝试访问其上的 filieres
属性(它没有)。相反,您应该获取 School 模型的实例。如果您正确定义了关系,Laravel 将负责为您找到枢轴,因此您通常不会直接访问枢轴模型,除非您有特殊原因。所以,改为在你的控制器中尝试这个:
public function show($id)
{
$establissement = Etablissement::find($id);
$faculty = $establissement->filieres;
return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissement'));
}
然后在您看来:
@foreach($faculty as $filiere)
有更简洁的方法来做到这一点(例如,查看一些关于路由模型绑定的教程),但这至少应该解决您当前代码的错误。
我的学校应用程序中有一个小应用程序。我想展示一所学校的所有院系。事实上,在我的数据库中,一所学校可以有一个或多个学院,一个学院可以属于一个或多个学校。
模范学校:
public function filieres ()
{
return $this->belongsToMany('App\Models\Filiere','etablissement_filieres','id_etablissements','id_filieres');
}
public function etablissement_filieres(){
return $this->hasMany('App\Models\Etablissement_filiere');
}
protected $fillable = [
'nom', 'image', 'ville', 'adresse', 'contact_1', 'contact_2',
'email', 'logo', 'presentation', 'brochure', 'localisation',
];
型号table枢轴Etablissement_filiere
:
public function filiere(){
return $this->belongsTo('App\Models\Filiere');
}
protected $fillable = [
'id', 'id_etablissements', 'id_filieres', 'prise_en_charge', 'prix',
];
模型文件:
public function etablissements ()
{
return $this->belongsToMany(Etablissement::class,'etablissement_filiere');
}
protected $fillable = [
'nom', 'diplome_requis', 'diplome_obtenu', 'prix', 'duree', 'type',
];
控制器:
public function show($id)
{
$faculty = Etablissement_filiere::where('id_etablissements','=','$id')->get();
return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissements'));
}
Blade 查看:
@foreach($faculty->filieres as $filiere)
<div class="container bg-fil py-4 my-5">
<div class="row pl-5">
<div class="col-md-9">
<h6 class="font-weight-bold">{{ $filiere ->nom}} <br>
<span class="text-primary"> {{ $filiere ->diplome_obtenu}}</span></h6>
</div>
<div class="col-md-3 pt-n5">
<img src="{{asset($etablissement->image)}}" alt="">
</div>
</div>
<div class="row pl-5 mt-md-n5">
<div class="col-md-6">
<h6> <strong> Diplôme réquis</strong>: {{ $filiere ->diplome_requis}} <br>
<strong>Durée</strong>: {{ $filiere ->duree}} <br>
<strong>Montant de la formation</strong>: {{ $etablissement_filieres ->prix}}</h6>
</div>
<div class="col-md-6">
<h6> <strong> Mode d'etude</strong>: {{ $filiere ->type}} <br>
<strong>Prise en charge</strong>: {{ $etablissement_filieres ->prise_en_charge}}</h6>
</div>
</div>
<div class="row pl-5 mt-4">
<div class="col-md-6">
<a href="{{ route('inscription') }}" class="btn btn-success font-weight-bold w-75 now">INSCRIVEZ VOUS MAINTENANT</a>
</div>
</div>
</div>
@endforeach
我正在尝试显示学校的所有院系,但出现此错误:
Property [filieres] does not exist on this collection instance.
你能告诉我防止错误的地方吗?
将代码更改为
public function show($id)
{
$faculty = Etablissement_filiere::where('id_etablissements','=',$id)->first();
return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissements'));
}
您的直接视图错误是当它试图计算 $faculty->filieres
.
调用类似 Model::where()->get()
return 模型的集合实例(类似于可迭代组),而不是单个模型。即使该集合只包含一个模型,它仍然是一组而不是模型本身。
因此,$faculty = Etablissement_filiere::where('id_etablissements','=','$id')->get();
return 是一个 集合 ,而不是单个模型。单个模型可能有 filieres
属性,但 Collection 实例(它本身是 Laravel 中的 class)没有。所以,如果你想要的只是第一个结果,而不是一个集合,那么以 ->first()
而不是 ->get()
.
然而,即使你解决了这个问题,你还有第二个问题。您正在获取数据透视模型 Etablissement_filiere
的实例,然后尝试访问其上的 filieres
属性(它没有)。相反,您应该获取 School 模型的实例。如果您正确定义了关系,Laravel 将负责为您找到枢轴,因此您通常不会直接访问枢轴模型,除非您有特殊原因。所以,改为在你的控制器中尝试这个:
public function show($id)
{
$establissement = Etablissement::find($id);
$faculty = $establissement->filieres;
return view('etablissements/edhec/touteslesfilieresedhec', compact('faculty','etablissement'));
}
然后在您看来:
@foreach($faculty as $filiere)
有更简洁的方法来做到这一点(例如,查看一些关于路由模型绑定的教程),但这至少应该解决您当前代码的错误。