简体   繁体   中英

Middleware For actions into Controller, Laravel 5.1

I have a Controller that have a index method, but also have a multiple levels of users that have access to this method, but single the admin can view all records of the database How can i add a middleware for select the action corresponding to user? I have the next code

<?php

namespace SET\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redirect;
use SET\Http\Requests;
use SET\Http\Requests\LabRequest;
use SET\Http\Controllers\Controller;
use Illuminate\Routing\Route;
use SET\Lab;
use Validator;
use Auth;
use DB;

class LaboratorioController extends Controller
{
public function __construct(){
    $this->beforeFilter('@find', ['only' => ['show','edit','update','destroy']]);
}

public function find(Route $route){
    $this->laboratorio = Lab::findOrFail($route->getParameter('laboratorio'));
}
/**
 * Display a listing of the resource.
 *
 * @return Response
 */

public function index()
{
    $labs = Lab::all();
    return view('comp.lab.index',['labs' => $labs]);
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    return view('comp.lab.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store(LabRequest $request)
{       Lab::create($request->all());
        Session::flash('message','Laboratorio creado correctamente');
        return Redirect::to('/laboratorio');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show()
{
     $teams = $this->laboratorio;
    return view('comp.lab.show',['lab'=>$this->laboratorio]);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit()
{
    return view('comp.lab.edit',['lab'=>$this->laboratorio]);
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update(LabRequest $request)
{
    $this->laboratorio->update($request->all());
    Session::flash('message','El laboratorio se actualizo correctamente');
    return Redirect::to('/laboratorio');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy()
{
    $this->laboratorio->delete();
    Session::flash('message','El laboratorio fue eliminado correctamente');
    return Redirect::to('/laboratorio');
}

Thanks :D

Here's a general idea:

$user_group = $user::get_user_group($user_id)
if($user_group->group_id === 'something'){
    \\method to return data for group 'something'
}

Get data by groups

$user_data = $user::get_data_by_group_id($group_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM