php - Laravel 工作表用户特定不起作用

标签 php mysql laravel

我正在尝试构建一个网络应用程序,员工可以在其中查看他的工作时间。到目前为止,这一直有效,向您显示所有员工及其轮类的列表。

这很好,但我们希望只对管理员可用。正式员工只能访问他们的个人记录。

考虑到您无法在 foreach 循环中放置 if ,我个人已经没有选择了。因此,请告诉我是否可以在以下情况下做某事:

IF Auth::user()->admin == 1{

//Show all employee records

} ELSE

//Show only logged in employee's record

我强烈建议您查看示例,以便更好地了解我正在尝试构建的内容。

时间 Controller

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

索引 View

@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 Illuminate\Database\Eloquent\Model;
use App\User;

class Hour extends Model
{
protected $fillable = [
    'id', 'werknemer_id', 'weeknummer', 'dag', 'uren', 'toelichting',
];

public function User() {
    return $this->belongsTo(User::class, 'werknemer_id');
}
}

当前管理 View 示例

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

然后将您的 View 更新为此,

@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

这应该有效

关于php - Laravel 工作表用户特定不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43207868/

相关文章:

php - 在 while 循环中从数据库中获取变量

mysql - 如何通过从操作系统提示符连接mysql来运行更新语句?

php - Laravel 中的@yield、@section

javascript - 使用 laravel 对象在 vue 模板中动态图像的数据绑定(bind)样式

php - 如何多次插入结果集?

php - 从 WooCommerce 中的当前产品类别获取同级术语 ID 列表

MySQL - 在内部连接中使用存储过程的结果

laravel - 如何在我的项目中设置惯性,当我尝试加载登录页面时出现错误

php - postgresql 不喜欢多个逗号

mysql - 在控制台中使用 mysql 无需密码,但在浏览器中使用密码