Php 如何在laravel中使用foreach循环仅显示一次数据…请编写代码
这是我的显示代码Php 如何在laravel中使用foreach循环仅显示一次数据…请编写代码,php,laravel,Php,Laravel,这是我的显示代码 @foreach($managefee as $managefees) @if($managefees->university==$university[0]->id) @if($managefees->eduId=="Undergraduate") @foreach($dept as $depts) @if($depts->id==$managefees->deptId)
@foreach($managefee as $managefees)
@if($managefees->university==$university[0]->id)
@if($managefees->eduId=="Undergraduate")
@foreach($dept as $depts)
@if($depts->id==$managefees->deptId)
<h4>{{$depts->name}}</h4>
@foreach($degree as $degrees)
@if($degrees->id==$managefees->degreeId)
<li>{{$degrees->name}}</li>
@endif
@endforeach
@endif
@endforeach
@endif
@endif
@endforeach
以下是我的控制器查询:
public function findUniversityDetailByCity($id) {
$uniid = Crypt::decrypt($id);
$heading = $uniid;
$university = manageUniversityModel::where('id', '=', $uniid)->get();
$city = addcity::all();
$managefee = managefee::all();
$degree = manageDegreeModel::all();
$dept = uniFormDepartment::all();
$country = addCountry::all();
$edulevel = edulevel::all();
return view('findUniversityDetailByCity', compact('university', 'city', 'managefee', 'degree', 'dept', 'country', 'edulevel'));
}
manageUniversityModel.php
managefee.php
控制器:
视图:
PS:学习使用数据库关系和联接。你本可以制作更好的数据库结构。显示你的控制器查询?我会创建一个部门拥有多个学位的关系,这样,depts将包含您的学位,并且每个学位只显示一次。这里是我的控制者query@Ali请编辑您的问题张贴您的控制器功能清楚@Hamelraj请看图片…这是我的控制器查询…在问题张贴
public function findUniversityDetailByCity($id) {
$uniid = Crypt::decrypt($id);
$heading = $uniid;
$university = manageUniversityModel::where('id', '=', $uniid)->get();
$city = addcity::all();
$managefee = managefee::all();
$degree = manageDegreeModel::all();
$dept = uniFormDepartment::all();
$country = addCountry::all();
$edulevel = edulevel::all();
return view('findUniversityDetailByCity', compact('university', 'city', 'managefee', 'degree', 'dept', 'country', 'edulevel'));
}
// this is how you create relationship between university and managefee or whatever...
public function managefees() {
return $this->hasMany(managefee::class, 'university', 'id');
}
public function departments() {
return $this->hasMany(uniFormDepartment::class, 'deptId', 'id');
}
public function degrees() {
return $this->hasMany(manageDegreeModel::class, 'degreeId', 'id');
}
public function findUniversityDetailByCity($id) {
// I don't know why would you use Crypt here?
$uniid = Crypt::decrypt($id);
$heading = $uniid;
// you can use find(), this will fetch first model by id
$university = manageUniversityModel::find($uniid);
// instead of fetch every single row from the database, use where cluse
// and using with() allows you to load related models.
$managefees = managefee::with('departments', 'degrees')
->where('university', $uniid)
->where('eduId', 'Undergraduate')->get();
// You don't need to fetch them separately, let Laravel do it for you.
// $degree = manageDegreeModel::all(); <-- this is already loaded in managefees
// $dept = uniFormDepartment::all(); <-- this is already loaded in managefees
// I would not recommend fetching every row at the same, instead use limits and pagination.
$city = addcity::all();
$country = addCountry::all();
$edulevel = edulevel::all();
return view('findUniversityDetailByCity', compact('university', 'city', 'managefees', 'country', 'edulevel'));
}
@foreach($managefees as $managefee)
// you don't need to check for ids manually
@foreach($managefee->departments as $department)
<h4>{{ $department->name }}</h4>
// do this only if you have same degrees in every department
@foreach($managefee->degrees as $degree)
<li>{{ $degree->name }}</li>
@endforeach
@endforeach
@endforeach