我正在尝试构建一个Web应用程序,员工可以在其中查看其工作时间。到目前为止,这仍然有效,向您显示所有员工及其工作班次的列表。
这很好,但我们希望它只提供给管理员。正式员工只能访问他们的个人记录。
考虑到您无法在 foreach 循环中放置 if,我个人已经没有选择余地了。因此,请让我知道是否可以在以下上下文中做某事:
IF Auth::user()->admin == 1{
//Show all employee records
} ELSE
//Show only logged in employee's record
我强烈建议您查看示例,以便更好地了解我正在尝试构建的内容。
小时控制器
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHour;
use AppUser;
class HoursController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$hours = Hour::all();
return view('hour.index', compact('hours', 'specificQuery'));
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
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 IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
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 IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
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
小时数据库模型
namespace App;
use IlluminateDatabaseEloquentModel;
use AppUser;
class Hour extends Model
{
protected $fillable = [
'id', 'werknemer_id', 'weeknummer', 'dag', 'uren', 'toelichting',
];
public function User() {
return $this->belongsTo(User::class, 'werknemer_id');
}
}
管理视图的当前示例
https://gyazo.com/fa75c6eb28da26d1adbc451e999dc853
您可以
尝试的一种解决方案是稍微改变一下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
这应该有效