首先声明一下,我已经查看了大部分与我的问题相关的答案,但是我仍然遇到了一个未定义的变量:user。
我想在仪表盘上显示已注册的用户,之前我使用过这段代码,它可以工作,但对于这个应用程序却不行。
Undefined variable: user (View: /Applications/MAMP/htdocs/eli/resources/views/dashboard.blade.php)
这里是我的代码:
UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Http\Requests;
class UserController extends Controller
{
public function getWelcome()
{
return view('welcome');
}
public function getDashboard()
{
$users = User::all();
return view('dashboard', compact('users'));
}
public function userSignUp(Request $request)
{
$this->validate($request, [
'email' => 'required|email|unique:users',
'first_name' => 'required|max:120',
'password' => 'required|min:4'
]);
$email = $request['email'];
$first_name = $request['first_name'];
$password = bcrypt($request['password']);
$user = new User();
$user->email = $email;
$user->first_name = $first_name;
$user->password = $password;
$user->save();
return redirect()->route('dashboard');
}
public function postSignin(Request $request)
{
$remember = $request->input('remember_me');
if(Auth::attempt(['email'=> $request['email'], 'password' => $request['password']], $remember )){
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
dashboard.blade.php
@extends('layouts.layout')
@section('title')
Dashboard
@endsection
@section('content')
<div class="container eli-main">
<div class="row">
<div class="col-md-6 col-md-12">
<h1>{{$user->username}}</h1>
</div>
</div>
@endsection
$users
来获取用户详细信息。 - linktoahref