我正在尝试构建一个Web应用程序,员工可以查看其工作时间。这项工作到现在为止,向您显示所有员工及其工作班次的清单。
这很好,但我们希望只有管理员才能使用。普通员工只能访问他们的个人记录。
考虑到你不能在foreach循环中放置一个if,我个人正在用完选项。所以,如果可以在以下情况下做某事,请告诉我。
for /d /r "%root%" %%a in (*) do if /i "%%~nxa"=="%TG%" set "folderpath=%%a"
我强烈建议您查看示例,以便更好地了解我要构建的内容。
小时控制器
IF Auth::user()->admin == 1{
//Show all employee records
} ELSE
//Show only logged in employee's record
INDEX VIEW
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Hour;
use App\User;
class HoursController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$hours = Hour::all();
return view('hour.index', compact('hours', 'specificQuery'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$users = User::pluck('name','id');
$huidigeWeek = date("W");
$huidigeDag = date("l");
return view('hour.create', compact('users', 'huidigeWeek', 'huidigeDag'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$hour = new Hour;
$hour->fill($request->all());
$hour->save();
return redirect('/uren')->with('success');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$hour = Hour::all()->find($id);
return view('hour.show', compact('hour'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user = User::pluck('name', 'id');
$hour = Hour::all()->find($id);
return view('hour.edit', compact('hour','user'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$hour = Hour::all()->find($id);
$hour->update($request->all());
return redirect('/uren/' . $id)->with('succes');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$hour = Hour::all()->find($id);
$hour->delete();
return redirect('/uren/')->with('succes');
}
}
小时数据库模型
@extends('layouts.app')
@section('content')
<div class="container">
<table class="display table table-bordered table-condensed table-responsive dynamic-table">
<thead>
<tr>
<th>Werknemer</th>
<th>Week</th>
<th>Dag</th>
<th>Aantal uren</th>
<th>Toelichting</th>
</tr>
</thead>
<tbody>
<br>
@if(Auth::user()->admin == 1)
@foreach($hours as $hour)
<tr class="clickable-row" data-url="/uren/{{ $hour->id }}">
<td>{{ $hour->User->name}}</td>
<td>{{ $hour->weeknummer }}</td>
<td>{{ $hour->dag }}</td>
<td>{{ $hour->uren }}</td>
<td>{{ $hour->toelichting }}</td>
{!! Form::close() !!}
</tr>
@endforeach
@endif
</tbody>
</table>
<br>
<a href="/home">Keer terug naar het dashboard.</a>
</div>
@endsection
管理员视图的当前示例
答案 0 :(得分:0)
您可以尝试的一种解决方案是稍微更改index()
方法,
public function index()
{
$allHours = [];
$memberSpecificHours = [];
if(Auth::user()->admin == 1) {
$allHours = Hour::all();
} else {
$memberSpecificHours = Hour::where('user_id', Auth::user()->id)->get();
}
return view('hour.index', compact('allHours', 'memberSpecificHours', 'specificQuery'));
}
然后将您的视图更新为
@if(Auth::user()->admin == 1)
@foreach($allHours as $hour)
<tr class="clickable-row" data-url="/uren/{{ $hour->id }}">
<td>{{ $hour->User->name}}</td>
<td>{{ $hour->weeknummer }}</td>
<td>{{ $hour->dag }}</td>
<td>{{ $hour->uren }}</td>
<td>{{ $hour->toelichting }}</td>
{!! Form::close() !!}
</tr>
@endforeach
@else
@foreach($memberSpecificHours as $hour)
<tr class="clickable-row" data-url="/uren/{{ $hour->id }}">
<td>{{ $hour->User->name}}</td>
<td>{{ $hour->weeknummer }}</td>
<td>{{ $hour->dag }}</td>
<td>{{ $hour->uren }}</td>
<td>{{ $hour->toelichting }}</td>
{!! Form::close() !!}
</tr>
@endforeach
@endif
这应该有效