Search code examples
phplaravelvariableslaravel-5undefined

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


SOLVED

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

image description

sorry for bad English :(


Solution

  • @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>