6

Just to preface this, i have went through most of the answers that are aligned to my question, pretty much i have a undefined variable for user.

I want to be able to display the registered user on the dashboard, i used this code before and it worked but not for this application.

Undefined variable: user (View: /Applications/MAMP/htdocs/eli/resources/views/dashboard.blade.php)

Here is my code,

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
3
  • You need to loop through $users in your view to get user details Commented Apr 9, 2017 at 6:26
  • thanks and it gives me SQLSTATE[HY000] [2002] Connection refused Commented Apr 9, 2017 at 6:31
  • 1
    nevermind i fixed it thanks Commented Apr 9, 2017 at 6:34

2 Answers 2

3

You're passing $users collection to the view, so you need to iterate over it if you want to display names of all users:

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

If you want to display name of authenticated user, just do this instead:

{{ auth()->user()->username }}
Sign up to request clarification or add additional context in comments.

Comments

0

i have done like this but it still dosen't helps me i have used foreach in my blade file and i have used users in my controller but still the same error

2 Comments

Try to add this as a comment rather than another answer.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.