phpmysqllaravellaravel-bladelaravel-datatables

The page on the site stopped working after I added the output of the table from the database to the page of the site


I made a display of the plate on the page of the site and then the following error appeared (error screenshot below) error text:

Undefined variable: users (View: /home/vagrant/code/bacon/resources/views/users.blade.php)

My 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');
    }
}

My web.php(routes):

<?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');

My Model 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',
    ];
}

My Controller 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)
    {
        //
    }
}

My 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');
    }
}

My users.blade.php (view page):

@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

my error screenshot


Solution

  • You need to pass $user variable to your view

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

    Or

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

    Or

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

    Read more about passing data to views https://laravel.com/docs/8.x/views#passing-data-to-views