如何在laravel中使用foreach循环仅显示一次数据。...请代码

时间:2018-06-24 16:17:42

标签: 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)
                  <h4>{{$depts->name}}</h4>

                @foreach($degree as $degrees)
                    @if($degrees->id==$managefees->degreeId)
                       <li>{{$degrees->name}}</li>
                    @endif
                  @endforeach

             @endif
             @endforeach

        @endif

    @endif
@endforeach

预期输出:

expected output picture

在此计算机科学和工程学中是主要类别,其他是子类别...我只想显示一个时间类别,其余的子类别应根据类别显示在后面。...但是当子类别时类别不断重复添加了。...我想一直只显示一个时间类别。...

这是我的数据库表:

+----+--------+--------+----------+------------+----------+----------------------------+-----+---------------+-----+------------+
| id |  fee   | deptId | degreeId | university | hec_dept |          degreeR           | hec |     eduId     | pec | deleted_at |
+----+--------+--------+----------+------------+----------+----------------------------+-----+---------------+-----+------------+
|  1 | 250000 |      1 |        6 |          1 |        1 | lorem ipsum dolor sit      | HEC | Undergraguate | PEC | null       |
|  2 | 230000 |      1 |        7 |          1 |        1 | lorem ipsum dolor sit amet | HEC | Undergraguate | PEC | null       |
+----+--------+--------+----------+------------+----------+----------------------------+-----+---------------+-----+------------+

这是我的控制器查询:

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

1 个答案:

答案 0 :(得分:0)

manageUniversityModel.php

// this is how you create relationship between university and managefee or whatever...
public function managefees() {
    return $this->hasMany(managefee::class, 'university', 'id');
}

managefee.php

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

PS :学习使用数据库关系和联接。您本可以使数据库结构更好。