【问题标题】:Undefined variable: users (View: C:\xampp\mysql\CRUD\resources\views\Admin\Form User\index.blade.php)未定义变量:用户(查看:C:\xampp\mysql\CRUD\resources\views\Admin\Form User\index.blade.php)
【发布时间】:2020-09-23 08:46:54
【问题描述】:

请帮助我。我是 Laravel 的新手。 我正在尝试从我的表中查看数据,但它显示了这个错误。我已经在控制器中定义了模型。我将 index.blade.php 放在文件中。 这些是我的代码。我使用 Laravel 7

app -> User.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    protected $table = "users";
}

app -> Http -> 控制器 -> AdminController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class AdminController extends Controller
{

public function user(Request $request){

        return view('Admin/Form User/index');
}

public function show()
    {
        $users = User::all();
        return view('index', ['index' => $users]);
    }

}

路由 -> web.php

Route::get('/Admin/Form User/index', 'AdminController@user');
Route::get('/index', 'AdminController@show');

资源 -> 视图 -> main.blade.php

<a href="Admin/Form User/index" class="btn btn-primary" role="button">Form User</a>

资源 -> 视图 -> 管理员 -> 表单用户 -> index.blade.php

<table class="table table-bordered table-hover table-striped">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Class</th>
                                <th>Name</th>
                                <th>Option</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($users as $u)
                            <tr>
                                <td>{{ $u->class }}</td>
                                <td>{{ $u->name }}</td>
                                
                                <td>
                                    <a>Edit</a>
                                    <a>Delete</a>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>

谢谢

【问题讨论】:

    标签: model-view-controller crud php-7 laravel-7


    【解决方案1】:

    您应该尝试以这种方式在 AdminController 中传递您的对象,

     public function show()
            {
                $users = User::all();
                return view('index')->with('users', $users);
            }
    

    【讨论】:

    • 谢谢你,它现在可以工作了,因为我在 AdminController 中用这个编辑了 :)
    【解决方案2】:

    您应该尝试在您的用户模型文件中使用这种方式。我认为它会工作

     <?php
        
        namespace App;
        
        use Illuminate\Database\Eloquent\Model;
        class User extends Model
        {
            protected $fillable = [
                'name', 'class',
            ];
        }
    

    【讨论】:

    • 感谢您的快速响应。我已经在 User.php 中更改了它,但仍然没有工作。我在 Laravel 中的错误显示在资源行 -> 视图 -> 管理员 -> 表单用户 -> index.blade.php :@foreach($users as $u)
    猜你喜欢
    • 2019-08-06
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多