Laravel views does not work Session: :( 'message'), nor the Auth :: user () -

2

I have a problem with Laravel 5.2.

At the moment of authenticating the user and redirecting to the home or login depending, the messages that I create by Session::flash() do not appear and on the home page when I try to show the name of the authenticated user using Auth::user()->show_name I get an error.

I tried inside the controller that I created for the authentication and the values show them to me, but when I redirect to the home or to the login it does not show anything.

What error do I have or what do I see in the view or in the drivers?

Model :: User.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $table = 'users_panel';

    protected $fillable = [
        'show_name', 
        'user', 
        'password', 
    ];

    protected $hidden = [
        'password', 'is_admin',
    ];
}

routes.php

<?php

Route::get('/', function () {
    return view('welcome');
});

Route::get('/orders', function () {
    return view('tp-admin/index', ['class' => 'login contrast-background']);
});

Route::resource('log', 'LogController');
Route::resource('logout', 'LogController@logout');

Route::get('/orders-panel/home', 'HomeController@index');

Route::group(['middleware' => ['web']], function () {

});

LogController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use Session;
use Redirect;

use App\User;
use App\Http\Requests;

class LogController extends Controller
{

    public function store(Request $request)
    {
        if($this->LogIn(['user' => $request['user'], 'password' => $request['password']], $request['remember_me'])){

            Session::flash('message', 'Sirve');
            //return Redirect::to('/orders-panel/home');
//-------------------Aqui Mostre  que si estaban llegando los datos a los mensaje
            if(Auth::check()):
                echo Auth::user()->show_name;
                echo '<br>';
                echo Session::get('message');
            endif;

        }else{
            Session::flash('message', 'Error');
//-------------------Aqui Mostre  que si estaban llegando los datos a los mensaje
            echo Session::get('message');
            //return Redirect::to('/orders');
        }
    }

    public function logout()
    {
        Auth::logout();
        return Redirect::to('/orders');
    }

    private function LogIn(array $credentials = [], $remember = false)
    {
        if ($this->ValidCredentials($credentials)) {
            if($this->ValidUser($credentials)){
                Auth::login($this->ValidUser($credentials), $this->ValidRemember($remember));
                return  true;
            }
        }
        return  false;
    }

    private function ValidCredentials($credentials)
    {
        if (($credentials['password'] != '') and ($credentials['user'] != '')) {
            return  true;
        }
    }

    private function ValidRemember($remember = false)
    {
        if(isset($remember)){
            return  true;
        }
    }

    private function ValidUser($credentials)
    {
        return  User::where('user', $credentials['user'])
                    ->where('password', md5($credentials['password']))
                    ->first();
    }

}

HomeController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function __construct()
    {

    }

    public function index()
    {
        return view('tp-admin/home'); 
    }
}

home.blade.php

@extends('layouts.app')

@section('content')
    <p>
    Aqui deveria salir el mensaje de Session::
        @if(Session::has('message'))
            {{Session::get('message')}}
        @endif
    </p>
    {!! trans('panel.welcome') !!}
    <p>
        <a href="/logout">LogOut</a>
    </p>
@endsection
    
asked by DevMaster732 06.03.2016 в 19:08
source

2 answers

0

You are not including the session in your routes, for this you must group them (those that need the session) with the middleware web, this is new from Laravel 5.2:

Route::group(['middleware' => ['web']], function () {
    // Las rutas que necesitan de la sesión aquí
});
    
answered by 15.03.2016 / 19:48
source
0

I'm a novice in laravel, but in what I've learned, I think middleware lacks this

Route::group(['middleware' => ['web']], function () {
    //
  });

  Route::get('admin/auth/login',[
    'uses' => 'Auth\AuthController@getLogin',
    'as' => 'admin.auth.login'
  ]);

  Route::post('admin/auth/login',[
    'uses' => 'Auth\AuthController@postLogin',
    'as' => 'admin.auth.login'
  ]);

  Route::get('admin/auth/logout',[
    'uses' => 'Auth\AuthController@logout',
    'as' => 'admin.auth.logout'
  ]);

Also this is my header of all my views

Route::group(['prefix'=>'admin','middleware' => 'auth'],function(){
    
answered by 08.03.2016 в 01:13