【问题标题】:how to show only one time data using foreach loop in laravel....please code如何在 laravel 中使用 foreach 循环仅显示一次数据....请编码
【发布时间】:2018-12-03 08:05:12
【问题描述】:

这是我的显示代码

 @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

预期输出:

在这个计算机科学和工程是主要类别,其他是子类别...我只想显示一个时间类别,其余子类别应该根据类别落后....但是当子类别时类别会一次又一次地重复已添加....我想一直只显示一个时间类别显示....

这是我的数据库表:

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

【问题讨论】:

  • 显示你的控制器查询?
  • 我会创建一个关系,其中部门有很多学位,这样部门将包含你的学位,并且每个学位只显示一次
  • 这是我的控制器查询
  • @Ali 请编辑您的问题,清楚地发布您的控制器功能
  • @Hamelraj 请看图片....这是我的控制器查询...有问题的帖子

标签: php laravel


【解决方案1】:

ma​​nageUniversityModel.php

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

ma​​nagefee.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:学习使用数据库关系和联接。你可以做出更好的数据库结构。

【讨论】:

    猜你喜欢
    • 2018-10-08
    • 2017-01-13
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多