Home Undefined variable: users (View: C:\xampp\htdocs\rentalmobil\resources\views\admin\user.blade.php)
Reply: 4

Undefined variable: users (View: C:\xampp\htdocs\rentalmobil\resources\views\admin\user.blade.php)

redwhale21
1#
redwhale21 Published in 2017-11-15 01:02:38Z

i am confusing that i think i have already make a true code with laravel. i have seen on many references for me to know how to read database. yes i'm newbie and still learning. well here is my code

from model. admin.php is the name of folder:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Admin extends Model
{
    protected $table = "admin";
}

here is my controller. my controller name is UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Admin;

class UserController extends Controller
{
    public function users(){
        $users = Admin::all();
        return view('admin.user', ['admin' => $users]);
    }

}

and here is my view, it is located in view/admin/ named user.blade.php:

@extends('admin.header')

@section('content')
<div class="col-md-12">
    <div class="card">
        <div class="header">
            <h4 class="title">Striped Table</h4>
            <p class="category">Here is a subtitle for this table</p>
        </div>
        <div class="content table-responsive table-full-width">
            <table class="table table-striped">
                <thead>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Salary</th>
                    <th>Country</th>
                    <th>City</th>
                </thead>
                <tbody>
                    <tr>
                        <td>1</td>
                        <td>{{ $users->email }}</td>
                        <td>$36,738</td>
                        <td>Niger</td>
                        <td>Oud-Turnhout</td>
                    </tr>
                </tbody>
            </table>

        </div>
    </div>
</div>
@endsection

and here is my error

i call from my browser with this http://localhost:8000/users

sorry for bad English :(

ceejayoz
2#
ceejayoz Reply to 2017-11-15 01:05:50Z

First, the variable will be called admin in the view, because that's how you passed it when you wrote 'admin' => $users.

Second, it's going to be a collection of users, so $users->email isn't going to work. You need to @foreach through each user.

MONSTEEEEEEEER
3#
MONSTEEEEEEEER Reply to 2017-11-15 03:14:18Z
// This will be your Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Admin;

class UserController extends Controller
{
    public function users(){
        $users = Admin::all();
        return view('admin.user)
             ->with("users", $users);
    }

}
//end controller

//your blade
@extends('admin.header')

@section('content')
<div class="col-md-12">
    <div class="card">
        <div class="header">
            <h4 class="title">Striped Table</h4>
            <p class="category">Here is a subtitle for this table</p>
        </div>
        <div class="content table-responsive table-full-width">
            <table class="table table-striped">
                <thead>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Salary</th>
                    <th>Country</th>
                    <th>City</th>
                </thead>
                <tbody>
                  @foreach($users as $user)
                    <tr>
                        <td>1</td>
                        <td>{{ $user['email'] }}</td>
                        <td>$36,738</td>
                        <td>Niger</td>
                        <td>Oud-Turnhout</td>
                    </tr>
                  @endforeach()
                </tbody>
            </table>

        </div>
    </div>
</div>
@endsection
//end blade
RamAnji
4#
RamAnji Reply to 2017-11-15 04:17:42Z
@foreach($admin as $users) 
<tbody>
                    <tr>
                        <td>1</td>
                        <td>{{ $users->email }}</td>
                        <td>$36,738</td>
                        <td>Niger</td>
                        <td>Oud-Turnhout</td>
                    </tr>
                </tbody>
@endforeach

if u not need for each

             <tbody>
                        <tr>
                            <td>1</td>
                            <td>{{ $admin[0]->email }}</td>
                            <td>$36,738</td>
                            <td>Niger</td>
                            <td>Oud-Turnhout</td>
                        </tr>
                    </tbody>
halfer
5#
halfer Reply to 2017-11-18 01:51:27Z

Please update your view/admin/user.blade.php and UserController like:

UserController:

class UserController extends Controller
{
    public function users(){
        $users = Admin::all();
        return view('admin.user', compact('users'));
    }

}

view/admin/user.blade.php:

<tbody>

 @foreach($users as $user)
   <tr>
       <td>1</td>
       <td>{{ $user->email }}</td>
       <td>$36,738</td>
       <td>Niger</td>
       <td>Oud-Turnhout</td>
   </tr>                
</tbody>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300832 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO