如何在Laravel中显示数据库中的数据

时间:2018-08-28 01:31:04

标签: php laravel-5

我正在尝试在管理端显示所有数据,但我失败了。怎么样 我可以显示它吗?

这是我要显示所有数据(工作流)的方法:

 public function getUserWorkflows(){
    $user = $this->userRepo->users();
    $active_module = AppConstants::$ACTIVE_MOD_USERS;
    $appraisals = Appraisal::with(
    ['academicBackgrounds','keyDuties','additionalAssignments',
    'competences','workflow', 'assignments','keyDuties'] )->
        where('user_id','=',$user->id)->orderBy('created_at','desc')->get();

    $summaries = DataGenerator::getAppraisalSummaries($appraisals);
    $user = User::with(['department'])->find($user->id);
    $appraisalsAssigned = DataGenerator::getAssingedAppraisals($user->id);
    $assignedSummaries = DataGenerator::getAppraisalSummaries($appraisalsAssigned);

    return view('user.tasks',compact('appraisals','users','active_module','summaries','appraisalsAssigned','assignedSummaries'));

    }

下面是我要显示数据的视图中的代码片段。但是它完全无法显示。因为我认为有些事情做对了:

 <div class="col s12">
    <div class="col s12">
    <ul id="issues-collection" class="collection">
    <li class="collection-item avatar">
    <i class="material-icons circle blue darken-4">folder</i>
    <span class="collection-header">Workflow</span>
    </li>

    @if(isset($appraisals) && count($appraisals) > 0)
    @foreach($appraisals as $appraisal)
    @if($loop->iteration <= 3)
    <li class="collection-item">
    <div class="row row-custom-modal-footer collections-title">
    <div class="col s12"><strong>{{$loop->iteration}}.&nbsp;</strong> 
 {{str_replace('_',' ',$appraisal->document_name)}}</div>
    </div>
    <div class="row row-custom-modal-footer valign-wrapper">
    <div class="col s3"><p>Date: <span class="text-primary">{{$appraisal->created_at}}</span></p></div>
    <div class="col s5">
    <span class="task-cat blue-stepper">
    @if(isset($summaries))
    @foreach($summaries as $summary)
    @if($summary['id'] == $appraisal->id)
    {{$summary['stage']}}
    @endif
    @endforeach
    @endif
    </span>
    </div>
    <div class="col s2">
    <div class="progress">
    <div class="determinate blue darken-4"
    @if(isset($summaries))
    @foreach($summaries as $summary)
    @if($summary['id'] == $appraisal->id)
    style="width: {{$summary['percentage']}};"
    @endif
    @endforeach
    @endif></div>
    </div>
    </div>
    <div class="col s1">
    <p class=" camel-case"><a class="text-primary bold-text" href="{{route('view_appraisal',[$appraisal->id])}}">View</a></p>
    </div>
    <div class="col s1">
    <p class=" camel-case"><a class="text-primary bold-text" href="{{route('view_appraisal',[$appraisal->id])}}">Stop Workflow</a></p>
    </div>
    </div>
    </li>
    @endif
    @endforeach
    <li class="collection-item">
    <div class="row">
    <div class="col s12 spacer-top">
    <a href="#">View all your tasks</a>
    </div>
    </div>
    </li>
    @else
    <li class="collection-item">
    <div class="col s12 bold-text">
    <p>There are no pending tasks</p>
    </div>
    </li>
    @endif
    </ul>
    </div>
    </div>

我希望当该人以管理员身份登录时,他们能够在访问视图后从数据库中查看所有数据。但是,仅当该人以用户身份登录时才显示数据。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,因此我只需要在控制器方法中进行以下编辑:

 public function getUserWorkflows(){
    $user = $this->userRepo->users();
    $active_module = AppConstants::$ACTIVE_MOD_USERS;
    // Edited this line of code    
$appraisals = Appraisal::with(
    ['academicBackgrounds','keyDuties','additionalAssignments',
    'competences','workflow', 'assignments','keyDuties'] )->
        orderBy('created_at','desc')->get();

    $summaries = DataGenerator::getAppraisalSummaries($appraisals);
    $user = User::with(['department'])->find($user->id);
    $appraisalsAssigned = DataGenerator::getAssingedAppraisals($user->id);
    $assignedSummaries = DataGenerator::getAppraisalSummaries($appraisalsAssigned);

    return view('user.tasks',compact('appraisals','users','active_module','summaries','appraisalsAssigned','assignedSummaries'));

    }

非常感谢,在仔细研究了控制器方法的代码行之后,我找到了解决方案