laravel Undefined variable:使用者

vuktfyat  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(136)

在此之前,我已经浏览了与我的问题一致的大部分答案,几乎我有一个undefined variable用户。
我希望能够显示在 Jmeter 板上的注册用户,我用这个代码之前,它的工作,但不适用于此应用程序。

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

zpgglvta

zpgglvta1#

你将$users集合传递给视图,所以如果你想显示所有用户的名字,你需要迭代它:

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

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

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

neekobn8

neekobn82#

我已经这样做了,但它仍然没有帮助我,我已经使用foreach在我的刀片文件,我已经使用用户在我的控制器,但仍然是同样的错误

相关问题