doubian19900911 2016-11-21 18:30
浏览 118

登录后如何显示用户名?

I am having a hard time trying to display the name or email of the user that it's logged in. I am not getting anything display. So far, I am able to logged in with the right credentials but I can't display the name. Could someone please tell me what I am doing wrong? I will really appreciate it.

Log In View:

@extends('main')

@section ('login')

<div class = "row">
    <div class="col-md-6">
        <h2>Log In </h2>
        <form action="{{route('signin')}}" method="post">
            <div class = "form-group">
                <label for="email">Please enter email address</label>
                <input class="form-control" type="text" name="email" id="email">
            </div>
            <div class="form-group">
                <label for="password"> Please enter the password</label>
                <input class="form-control" type="password" name="password" id="password">
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
            <input type="hidden" name="_token" value="{{Session::token() }}">
        </form>
    </div>
</div>


@endsection

My View where I will display the name

    @extends('main')

@section ('welcome')

<style>
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 200px;
    background-color: #f1f1f1;
}

li a {
    display: block;
    color: #000;
    padding: 8px 16px;
    text-decoration: none;
}

/* Change the link color on hover */
li a:hover {
    background-color: #555;
    color: white;
}

.imagen{
  width:600px;
  height:auto;
  margin-top: 20px;
  margin-bottom: 0px;
  margin-left: 40px;
}

.picture{
  margin-left: 550px;
  margin-top: -300px;
  padding: 10;
  width:600px;
  height:auto;
  display: inline-block;
  border-radius: 8px;
  border: 1px solid #ddd;
  padding:5px;
}

form {
                width:500px;
                margin:50px auto;
}
.search {
                padding:8px 15px;
                background:rgba(50, 50, 50, 0.2);
                border:0px solid #dbdbdb;
}
.button {
                position:relative;
                padding:6px 15px;
                left:-8px;
                border:2px solid #207cca;
                background-color:#207cca;
                color:#fafafa;
}
.button:hover  {
                background-color:#fafafa;
                color:#207cca;
}
.user{
    position:absolute;
    margin-top: -120px;
    right:20px;
    font-size:14px;
    font-family:"courier";
}

#logout{
  top:0;
  right:0;
  position:absolute;
  margin-top:15px;
  margin-right:2px;

}

</style>

<div class="something">
<img class ="imagen" src="http://casahogarsanvicentedepaul.com.mx/wp-content/uploads/2014/11/logonuevo1_casahogar-copia.jpg">
@if(Auth::check()) 
<p>Welcome {{ \Auth::user()->iduser}} !</p>
   @endif
    <form action="{{ url('user/logout') }}" method="GET">
  <div class = "form-group">
        <input type="submit" value="Logout" class="btn btn-primary" id="logout"/>
      </div>
    </div>
    </form>

 <hr>
<hr>


    <div class="container">
      <ul class="nav nav-pills nav-stacked">
        <li class="active"><a href="{{url('/') }}">Home</a></li>
        <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Inventory<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="{{url('food/list/') }}">Food</a></li>
            <li><a href="{{url('medicines/list/') }}">Medicines</a></li>
            <li><a href="{{url('inventory/list/') }}">Cleaning Items</a></li>
            <li><a href="{{url('inventory/list/') }}">School Supplies</a></li>
           <li><a href="{{url('inventory/list/') }}">Furniture</a></li>
          </ul>
        </li>
        <li><a href="{{url('kids/list/') }}">Kids</a></li>
        <li><a href="{{url('guardians/list/') }}">Guardians</a></li>
        <li><a href="{{url('donations/list/') }}">Donations</a></li>
        <li><a href="{{url('expenses/list/') }}">Expenses</a></li>
        <li><a href="{{url('users/list/') }}">Users</a></li>
      </ul>
    </div>


    <img class="picture" src="https://scontent-lax3-1.xx.fbcdn.net/v/t1.0-9/14141688_1829614530603926_739886400345934590_n.jpg?oh=4a1ccdcd13e81d3be099f8a1e2f422dc&oe=583D0E61">

@endsection

Routes:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/
    Route::get('/', function () {
    return view('login');
});

Route::post('/signin', [
    'uses' => 'UserController@postSignIn',
    'as' => 'signin'
    ]);

Route::get('/dashboard', [
    'uses'=>'UserController@getDashboard',
    'as'=> 'dashboard'
    ]);

Route::get('user/logout',function(){
    Auth::logout();
    return redirect('/');
});


Route::get('users/list', 'UserController@viewUsers');
Route::get('adduser/list','UserController@viewAddPage');
Route::post('/createuser', [
    'uses'=>'UserController@createUser',
    'as'=>'createuser']);
Route::get('deleteusers/{iduser}', 'UserController@delete');

UserController:

<?php

namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Redirect;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use App\Http\Controllers\Controller;
use Auth;





class UserController extends Controller
{

    public function getDashboard(){
        return view('welcome');
    }

    public function viewUsers(){
        $users = User::all();

        return view('users')->with('users', $users);
    }

    public function viewAddPage(){

        return view('add');
    }

    public function createUser(Request $request){
        $iduser = $request['iduser'];
        $first_name = $request['first_name'];
        $middle_name = $request['middle_name'];
        $last_name=$request['last_name'];
        $address = $request['address'];
        $email = $request['email'];
        $phone = $request['phone'];
        $position = $request['position'];
        $password = bcrypt($request['password']);

        $user = new User();

        $user->iduser = $iduser;
        $user->first_name = $first_name;
        $user->middle_name = $middle_name;
        $user->last_name = $last_name;
        $user->address = $address;
        $user->email = $email;
        $user->phone = $phone;
        $user->position = $position;
        $user->password = $password;

        $user->save();
        return redirect('users/list/');
    }

     public function delete($iduser){
        $user = DB::table('users')->where('iduser', $iduser)->delete();

        return redirect('users/list/');
    }

    public function postSignIn(Request $request)
    {
        if (\Auth::attempt(['email'=> $request['email'], 'password'=>$request['password']])){
            return redirect()->route('dashboard');
        }
        return redirect()->back();
    }

}

User Model:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'users';
    protected $fillable = [
        'iduser', 'first_name', 'middle_name', 'last_name', 'address', 'email', 'phone', 'position', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}
  • 写回答

4条回答 默认 最新

  • duanjiao3686 2016-11-21 18:32
    关注

    Try to use this to display first name:

    auth()->user()->first_name
    

    Also, I'd recommend you to change iduserback to id, because Laravel uses id as primary key for User model.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题