未定义变量:users(查看:C:\xampp\htdocs\rentalmobil\resources\views\admin\user.blade.php)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47297782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:33:18  来源:igfitidea点击:

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

phplaravelvariableslaravel-5.2undefined

提问by redwhale21

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

我很困惑,我认为我已经用 laravel 编写了一个真正的代码。我在很多参考资料上看到过让我知道如何阅读数据库。是的,我是新手,仍在学习。这是我的代码

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

从模型。admin.php 是文件夹的名称:

<?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

这是我的控制器。我的控制器名称是 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:

这是我的观点,它位于 view/admin/ 名为 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>,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

我用这个http://localhost:8000/users从我的浏览器调用

image description

图片说明

sorry for bad English :(

抱歉英语不好:(

回答by RamAnji

@foreach($admin as $users) 
<tbody>
                    <tr>
                        <td>1</td>
                        <td>{{ $users->email }}</td>
                        <td>,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>,738</td>
                            <td>Niger</td>
                            <td>Oud-Turnhout</td>
                        </tr>
                    </tbody>

回答by ceejayoz

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

首先,该变量将admin在视图中被调用,因为这就是你在编写'admin' => $users.

Second, it's going to be a collectionof users, so $users->emailisn't going to work. You need to @foreachthrough eachuser.

其次,它将是一个用户集合,因此$users->email不会起作用。您需要@foreach通过每个用户。

回答by AddWeb Solution Pvt Ltd

Please update your view/admin/user.blade.phpand UserControllerlike:

请更新您的view/admin/user.blade.phpUserController喜欢的:

UserController:

用户控制器

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

}

view/admin/user.blade.php:

查看/管理员/user.blade.php

<tbody>

 @foreach($users as $user)
   <tr>
       <td>1</td>
       <td>{{ $user->email }}</td>
       <td>,738</td>
       <td>Niger</td>
       <td>Oud-Turnhout</td>
   </tr>                
</tbody>