0

我在网站的页面上做了一个板块的显示,然后出现以下错误(错误截图如下)错误文本:

未定义变量:用户(查看:/home/vagrant/code/bacon/resources/views/users.blade.php)

我的 MainController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MainController extends Controller
{
    public function welcome()
    {
    return view('/welcome');
    }
    public function users()
    {
    return view('users');
    }
}

我的 web.php(路线):

<?php

    use App\Http\Controllers\UserController;
    use Illuminate\Support\Facades\Route;

    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */

    Route::get('/welcome', 'App\Http\Controllers\MainController@welcome');
    Route::get('/users', 'App\Http\Controllers\MainController@users');
    Route::get('/business', 'App\Http\Controllers\BusinessController@index');
    Route::post('/business', 'App\Http\Controllers\BusinessController@store');
    Route::get('/projects', 'App\Http\Controllers\ProjectsController@index');
    Route::post('/projects', 'App\Http\Controllers\ProjectsController@store');
    Route::get('/projects/create', 'App\Http\Controllers\ProjectsController@create');
    Route::get('/business/create', 'App\Http\Controllers\BusinessController@createbusiness');
    Auth::routes();

    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

我的模型 User.php:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

我的控制器UsersController.php:

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function users()
    {
    $users = User::all(); 
    return view('users' , ['users' => $users]) ; 
   }
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function show(Post $post)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function edit(Post $post)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Post $post)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function destroy(Post $post)
    {
        //
    }
}

我的 users_table.php:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

我的 users.blade.php(查看页面):

@extends('layouts.layout')
@section('title')Користувачі@endsection
@section ('main_content')
    <h1>Користувачі</h1>
    <p>
    <table class="table table-dark">
            <thead>
              <tr>
                <th scope="col">ID</th>
                <th scope="col">Name</th>
                <th scope="col">Mail</th>
                <th scope="col">Business</th>
              </tr>
            </thead>
            <tbody>
                @foreach ($users as $singleUsers)
                <tr>
                    <th scope="row">{{ $singleUsers->id}}</th>
                    <td>{{ $singleUsers->id}}</td>
                    <td>{{ $singleUsers->name}}</td>
                    <td>{{ $singleUsers->mail}}</td>
                    <td>{{ $singleUsers->business}}</td>
              </tr>
              @endforeach
            </tbody>
        </table>
    </p>
@endsection

我的错误截图

4

1 回答 1

0

您需要将 $user 变量传递给您的视图

public function users()
    {
    $users = User::all(); 
    return view('users' , compact('users'))  ;
    }

或者

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

或者

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

阅读有关将数据传递给视图的更多信息https://laravel.com/docs/8.x/views#passing-data-to-views

于 2021-02-02T12:39:20.880 回答