我很難嘗試顯示它登錄的用戶的名字或電子郵件。我沒有收到任何顯示。到目前爲止,我可以使用正確的憑據登錄,但無法顯示名稱。有人能告訴我我做錯了什麼嗎?我會非常感激。如何在用戶登錄後顯示用戶名?
登錄查看:
@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
我查看我要在那裏顯示的名稱
@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
路線:
<?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' => '[email protected]',
'as' => 'signin'
]);
Route::get('/dashboard', [
'uses'=>'[email protected]',
'as'=> 'dashboard'
]);
Route::get('user/logout',function(){
Auth::logout();
return redirect('/');
});
Route::get('users/list', '[email protected]');
Route::get('adduser/list','[email protected]');
Route::post('/createuser', [
'uses'=>'[email protected]',
'as'=>'createuser']);
Route::get('deleteusers/{iduser}', '[email protected]');
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();
}
}
用戶模型:
<?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',
];
}
將'{{\ Auth :: user() - > iduser}}'更改爲'{{Auth :: user () - >名字}}'? – snh
我已經這樣做了,仍然沒有顯示任何東西 –
請執行'dd(auth() - > user());'併發布結果。另外,也許你的用戶表結構會有所幫助。 –