gpt4 book ai didi

php - laravel undefined variable : user

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:40:33 28 4
gpt4 key购买 nike

作为序言,我已经完成了大部分与我的问题一致的答案,几乎我有一个 undefined variable为用户。

我希望能够在仪表板上显示注册用户,我之前使用过这段代码并且它有效但不适用于此应用程序。

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 集合传递给 View ,因此如果您想要显示所有用户的姓名,则需要对其进行迭代:

@foreach ($users as $user)
{{ $user->username }}
@endforeach

如果你想显示经过身份验证的用户的姓名,只需这样做:

{{ auth()->user()->username }}

关于php - laravel undefined variable : user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43303703/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com