Tuesday, April 4, 2017

Laravel worktable user specific not working

I am trying to build an web application where an employee can review his working times. This works until now, showing you a list of all employees and their workshifts.

This is nice and all, but we would like to have to have that only available to the admins. The regular Employees should only have access to their personal records.

I personally am running out of options considering you cannot place an if inside the foreach loop. So please let me know if it is possible to do something in the context of:

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

//Show all employee records

} ELSE

//Show only logged in employee's record

I highly advice you look at the example as to get a better idea of what I am trying to build.

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

INDEX 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/">
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        {!! Form::close() !!}
    </tr>
    @endforeach
    @endif
    </tbody>

</table>
<br>
<a href="/home">Keer terug naar het dashboard.</a>
</div>
@endsection

HOUR DATABASE MODEL

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

CURRENT EXAMPLE OF ADMIN VIEW

https://gyazo.com/fa75c6eb28da26d1adbc451e999dc853



via Justin Boxem

Advertisement