这是我的页面DepartmentController.php,我编写了一个函数来检查ID为0(根)和子代的部门。在我的数据库中,我有两个表用户,部门与他们之间存在一对多关系。部门可以但是我想显示部门内部的用户,我试图在这些功能中添加此用户,但是它只显示一个用户,而没有显示在正确的部门中。我也尝试添加此功能,但它给了我这个错误:未定义变量:用户
if(count($departments->$user)) {
echo '<br>' .$user->username.'</br>';
}
$users = DB::table('users')
->join('departments', 'users.department', '=', 'departments.id')
->select('users.id','users.lastname','users.name as username','departments.name')->get();
//print_r($users);exit;
foreach($users as $user)
{
// dd($user->username);
}
DepartmentsController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Department;
use App\User;
use Illuminate\Support\Facades\DB;
class DepartmentController extends Controller
{
public function treeView(){
$departments = Department::where('parent', '=', 0)->get();
$tree='<ul id="browser" class="filetree"><li class="tree-view"></li>';
foreach ($departments as $department) {
$tree .='<li class="tree-view closed"<a class="tree-name">'.$department->name.'</a>'; //first department
if(count($department->childs)) {
$tree .=$this->childView($department);// if this department has children
}
}
$tree .='</ul>';
//return $tree;
return view('admin.page',compact('tree'));
}
public function childView($department){
$html ='<ul>';
$users = DB::table('users')
->join('departments', 'users.department', '=', 'departments.id')
->select('users.id','users.lastname','users.name as username','departments.name')->get();
//print_r($users);exit;
foreach($users as $user)
{
// dd($user->username);
}
foreach ($department->childs as $arr) {
if(count($arr->childs))
{
$html .='<li class="tree-view closed"><a class="tree-name">'.$arr->name.'</a>';
$html.= $this->childView($arr);
}
else
{
$html .='<li class="tree-view" ><a class="tree-name">'.$arr->name.'</a>';
$html .="</li>";
}
}
$html .="</ul>";
return $html;
}
page.blade.php
@extends ('layouts.master')
@section('title')
Department Management | Admin
@endsection
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
@section('content')
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New department</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
</div>
<form action="/save-department" method="POST">
{{ csrf_field() }}
<div class="modal-body">
<div class="form-group">
<label for="recipient-name" class="col-form-label">Name</label>
<input type="text" class="form-control" name="name" id="recipient-name">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">ID</label>
<input type="text" class="form-control" name="id" id="recipient-id">// changed from name to id
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
<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"> Department and Employees </h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead class=" text-primary">
</thead>
</thead>
<tbody>
<div id="jstree" class="container">
{!! $tree !!}
</div>
</tbody>
</table>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<!-- 4 include the jQuery library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<!-- 5 include the minified jstree source -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script>
$(function () {
// 6 create an instance when the DOM is ready
$('#jstree').jstree();
// 7 bind to events triggered on the tree
$('#jstree').on("changed.jstree", function (e, data) {
console.log(data.selected);
});
// 8 interact with the tree - either way is OK
$('button').on('click', function () {
$('#jstree').jstree(true).select_node('child_node_1');
$('#jstree').jstree('select_node', 'child_node_1');
$.jstree.reference('#jstree').select_node('child_node_1');
});
});
</script>
@endsection
User.php
public function department()
{
return $this->belongsTo(Department::class);
}
然后您可以将该关系用于拥有部门的所有用户
$users = User::whereHas('department')->get(); foreach($users as $user) { // dd($user->username); }
如果要特定部门的用户,只需使用反向关系
foreach($department->users as $user) { // dd($user->username); }