1
votes

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 :(

3
Instead of a screenshot, try to copy/paste the error text. That will make easier for others to search for similar issues, and to quote if necessary when answering this question - rafahoro
variable for blade is $admin not $users. Add a loop also for $admin. - Bugfixer

3 Answers

2
votes
@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>
0
votes

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.

0
votes

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>