How do I access a view if it is a normal user and another view if it is an administrator using the same driver?

1

I wanted to access a view by means of a function in a controller if it was an administrator and in another view through that same function of the same controller for the user, but I do not know if this is possible or if it will give me problems in the long run , for now I had done the following:

public function store(LibroFormRequest $request)
{
    $libro= new Libro();
    $libro->lib_no=0;
    $libro->lib_titulo=$request->get('lib_titulo');
    $libro->lib_direccion_web=$request->get('lib_direccion_web');

    $autorid=$request->get('lib_autor_id');
    $autor=new Autor();

    if($autorid=="" || $autorid==0){
        $autor->atr_no=0;
        $usuario_actual=\Auth::user();
        $autor->atr_nombre_artistico=$usuario_actual->nombre;
        $autor->atr_cliente_id=$usuario_actual->id_usuario;
        $autor->atr_activo=1;
        $autor->save();

        $autorid=DB::table('autor')->orderBy('id_autor', 'desc')
            ->first();
        $autor2=Autor::findOrFail($autorid->id_autor);
        $autor2->atr_no=$autorid->id_autor+1;
        $autor2->update();
    }

    $fecha=$request->get('lib_fecha_creacion');

    if($fecha==""|| $fecha==null){
        $fecha=Carbon::now('Europe/Madrid');
    }

    $libro->lib_fecha_creacion=$fecha->toDateString();
    $libro->lib_precio=$request->get('lib_precio');
    $libro->lib_genero_literario_id=$request->get('lib_genero_literario');

    if($autorid=="" || $autorid==0){
        $libro->lib_autor_id=$autor->id_autor;
    }else{
        $libro->lib_autor_id=$autorid;
    }
    $libro->lib_activo=1;
    $libro->save();

    $libroid=DB::table('libro')->orderBy('id_libro', 'desc')
        ->first();
    $libro2=Libro::findOrFail($libroid->id_libro);
    $libro2->atr_no=$libroid->id_libro+1;
    $libro2->update();

    return Redirect::to('almacen/libro');
}

I do not know how I should add the new redirection since I'm using a resource type path:

Route::resource('almacen/libro', 'LibroController');

I would like you to throw a cable around here with the decision because I do not know what is the right thing to do in this case, so I ask you. Thanks for reading me up here.
Greetings.

    
asked by Maria Rosa Cambero 28.01.2018 в 17:11
source

1 answer

0

Of course it is possible.

Assuming you have these three fields in your Users table (obviously not the same):

+-----------+--------------+-------------+---------------+
|id_usuario |alias_usuario |tipo_usuario |contra_usuario |
|___________|______________|_____________|_______________|
|1          |dolar         |admin        |mucha_plata    |
|___________|______________|_____________|_______________|
|2          |peluso        |usuario      |pulgoso        |
|           |              |             |               |
+----------------------------------------+---------------+

Once your user has logged in, you will be able to access to its properties through this method:

Auth::user();

Operation

If you want to know what is the id of the logged in user you should do it like this:

Auth::user()->id_usuario

If you wanted to know what the user's counter could be, do it this way:

Auth::user()->contra_usuario

Solving your problem ...

If you would like to know when the user is admin or user limited you could get it this way:

Auth::user()->tipo_usuario 

To show the indicated view after knowing if it is limited user or admin you can implement an if / else on your controller:

if((Auth::check()) && (Auth::user()->tipo_usuario)=="admin"){
    //Es admin =)
    return redirect()->route('mi.vista.de.admin');
}elseif((Auth::check()) && (Auth::user()->tipo_usuario)=="usuario"){
    //Es usuario =|
    return redirect()->route('mi.vista.de.usuario');
}else{
    //No invitado =(
    return redirect()->route('mi.vista.de.logueo')
                     ->withErrors('Inicie sesión primero');
}
    
answered by 29.01.2018 / 20:12
source