I have a Laravel project with two tables in db,departments and users.The admin can delete departments and users.A user has one department and a department has many users.I want to check in the delete function that if a department has a user he cant be deleted.I have write this delete department function but it gives me this error ErrorException Trying to get property 'users' of non-object 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');
$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->integer('parent');
$table->timestamps();
});
}
DepartmentController.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 usersdep(){
//THIS IS THE ADDED CODE
$users = DB::table('users')
->join('departments', 'users.department', '=', 'departments.id')
->select('users.id','users.lastname','users.name as username','departments.name')->get();
return view('admin.pageusers')->with('users', $users);
//print_r($users);exit;
// foreach($users as $user)
// {
// return $user;
//dd($department->name);
//dd($user->username);
// }
//ADDED CODE
}
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>';
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></a>';
$html .="</li>";
}
}
$html .="</ul>";
return $html;
}
/* public function usersdep(){
$users = DB::table('users')
->join('departments', 'users.department', '=', 'departments.id')
->select('users.id','users.lastname','users.name as username','departments.name')->get();
$tree='<ul id="browser" class="filetree"><li class="tree-view"></li>';
foreach ($users as $user) {
$tree .='<li class="tree-view closed"<a class="tree-name">'.$user->name.'</a>'; //first department
if(count($user->childs)) {
$tree .=$this->childView($user);// if this department has children
}
}
$tree .='</ul>';
//return $tree;
return view('admin.pageusers',compact('tree'));
}*/
public function index()
{
$departments = \App\Department::all();
return view('admin.department')->with('departments', $departments);
}
public function store(Request $request)
{
$departments = new Department;
$departments->id = $request->input('id');
$departments->name = $request->input('name');
$departments->parent = $request->input('parent');
$departments->save();
return redirect('department')->with('status','Data added');
}
public function edit($id)
{
$departments = Department::findOrFail($id);
return view('admin.department-edit')->with('departments',$departments);
}
public function update(Request $request,$id)
{
$departments = Department::find($id);
$departments->id = $request->input('id');
$departments->name = $request->input('name');
$departments->parent = $request->input('parent');
$departments->update();
return redirect('/department')->with('status','Data updated');
}
public function delete($id)
{
$users = User::all();
$departments = Department::findOrFail($id);
$hasUser = false;
foreach ($departments as $department) {
if ($department->users->withTrashed()->count()) {
$hasUser = true;
break;
}
if ($hasUser)
{
$departments->delete();
//$this->delete();
}
else
{
$departments->forceDelete();
// $this->forceDelete();
}
}
//$departments->delete();
return redirect('/department')->with('status','Data deleted');
}
//menyre tjt
/*
function CategoryTree($output=null, $parent=0, $indent=null){
$departments = DB::table('departments')->get();
// show the departments one by one
//return $r;
foreach($departments as $dep){
$output = '<ul>';
if($dep->parent== 0){
$output .= '<li>'.$dep->name.'</li>';
} else {
}
}
while($c = $r->fetch(PDO::FETCH_ASSOC)){
return '12';
$output .= '<option value=' . $c['id'] . '>' . $indent . $c['name'] . "</option>";
if($c['id'] != $parent){
// in case the current departments's id is different that $parent
// we call our function again with new parameters
CategoryTree($output, $c['id'], $indent . " ");
}
}
// return the list of departments
return $output;
}
*/
}
User.php
public function department()
{
return $this->belongsTo(Department::class);
}
Department.php
public function users()
{
return $this->belongsTo(User::class);
}
department.blade.php
@extends ('layouts.master')
@section('title')
Department Management | Admin
@endsection
@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">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Parent ID</label>
<input type="text" class="form-control" name="parent" id="recipient-parent">
</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 Management </h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead class=" text-primary">
<th>Department</th>
<button type="button" class="btn btn-primary float-right" data-toggle="modal" data-target="#exampleModal" >Add</button>
<thead class=" text-primary">
<th>ID</th>
<th>Name</th>
<th>PID</th>
<th>Edit</th>
<th>Delete</th>
</thead>
</thead>
<tbody>
@foreach($departments as $department)
<tr>
<td>{{ $department->id }}</td>
<td>{{ $department->name }}</td>
<td>{{ $department->parent }}</td>
<td>
<a href="{{ url('department-edit/'.$department->id) }}" class="btn btn-success">Edit</a>
</td>
<td>
<form action="{{ url('department-delete/'.$department->id) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
<a href="{{ url('/page') }}" class="btn btn-success">See the Departments and Employees</a>
</tbody>
</table>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
@endsection
```[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/zbJ0u.jpg