我有两个表,部门和用户。一个部门有很多用户。一个用户属于一个部门。我试图编写一个foreach循环以显示其用户所在部门的名称,但出现此错误。
Facade \ Ignition \ Exceptions \ ViewException未定义的变量:部门(视图:C:\ xampp \ htdocs \ Laravel \ blog2 \ resources \ views \ admin \ dashboard.blade.php)
create_users_table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
//$table->text('avatar');
$table->string('name');
$table->string('lastname');
$table->string('phone');
$table->string('jobtitle');
$table->integer('department_id');
$table->timestamps();
$table->string('usertype')->nullable();
$table->string('email');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
});
}
create_departments_table
public function up()
{
Schema::create('departments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
Department.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Department extends Model
{
protected $table = 'departments';
protected $fillable = [
'name',
];
protected $primaryKey = 'id';
public function users()
{
return $this->hasMany(User::class);
}
}
User.php
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [];
/**
* 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',
];
public function users()
{
return $this->belongsTo(Department::class);
}
}
dashboard.blade.php
@extends ('layouts.master')
@section('title')
DASHBOARD | Admin
@endsection
@section('content')
<div class="row">
<div class="col-md-12">
<div class="card">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<div class="card-header">
<h4 class="card-title"> DASHBOARD </h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead class=" text-primary">
<th>ID</th>
<th>Name</th>
<th>Last Name</th>
<th>Department</th>
</thead>
<tbody>
@foreach($departments as $department)
<td> {{$department}}</td>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
@endsection
DashboardController.php
class DashboardController extends Controller
{
public function index()
{
$users= User::all();
$departments = Department::with('users')->first();
// return view('admin.dashboard')->with('departments',$departments)->with('users',$users);
// $departments = Department::with('users')->where('users.department_id','=','departments.id')->first();
// dd($departments);
dd($users);
// return view('admin.dashboard',compact('departments','users'));
}
}
web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::group(['middleware' => ['auth','admin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('/dashboard{id}', "Admin\DashboardController@index");
Route::get('/role-register','Admin\DashboardController@registered');
Route::get('/role-edit/{id}', 'Admin\DashboardController@registeredit');
Route::put('/role-register-update/{id}', 'Admin\DashboardController@registerupdate');
Route::delete('/role-delete/{id}', 'Admin\DashboardController@registerdelete');
Route::post('/save-user', 'Admin\DashboardController@store');
Route::get('/department', 'Admin\DepartmentController@index');
Route::post('/save-department', 'Admin\DepartmentController@store');
Route::get('/department-edit/{id}', 'Admin\DepartmentController@edit');
Route::put('/department-update/{id}', 'Admin\DepartmentController@update');
Route::delete('/department-delete/{id}', 'Admin\DepartmentController@delete');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
我也尝试过,但还是一样。
<td> {{$department->name}}</td>
@foreach($departments->name as $department)
<td> {{$department}}</td>
@endforeach
在您的web.php中,替换为:
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
您可以尝试这个吗?看看它是否改变了错误?