title: How to display parent name in Laravel
body: <p>I have a departments table in db with id,name, and parent.The parent is the id that corresponds to the parent root.Now i have displayed the id(parent id) but i want to show the name of the department that correspond with this id.I have tried the query at the departmentcontroller ,index function but it gives me this error 
  <strong>Illuminate\Database\QueryException
  SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'departments' (SQL: select <code>departments</code>.<code>id</code>, <code>departments</code>.<code>parent</code>, <code>departments</code>.<code>name</code> from <code>departments</code> inner join <code>departments</code> on <code>department</code>.<code>id</code> = <code>departments</code>.<code>parent</code> where <code>id</code> = parent)</strong></p>
  
  <p>DepartmentController.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?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($id)
    {
     $department = Department::with('users')-&gt;find($id);
     return view('admin.page-users')-&gt;with('users', $department-&gt;users);
    } 
     public function treeView()
    {
     $departments = Department::with('childs')-&gt;where('parent', 0)-&gt;get();  
     return view('admin.page',compact('departments'));
    } 
  
    public function index()
    {
      //$departments = \App\Department::all();  
      //return view('admin.department')-&gt;with('departments', $departments);
   return $departments =  DB::table('departments')
      -&gt;join('departments', 'department.id', '=', 'departments.parent')
     -&gt;select('departments.id','departments.parent','departments.name')-&gt;where('id','=','parent')-&gt;get();
    }
  
  </code></pre>
  
  <p>department.blade.php</p>
  
  <pre class="lang-php prettyprint-override"><code>@extends ('layouts.master')
  
  @section('title')
  Department Management | Admin
  @endsection
  @section('content')
  
  &lt;div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"&gt;
    &lt;div class="modal-dialog" role="document"&gt;
      &lt;div class="modal-content"&gt;
        &lt;div class="modal-header"&gt;
          &lt;h5 class="modal-title" id="exampleModalLabel"&gt;New department&lt;/h5&gt;
          &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;
            &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
          &lt;/button&gt;
          @if (session('status'))
          &lt;div class="alert alert-success" role="alert"&gt;
            {{ session('status') }}
          &lt;/div&gt;
          @endif
        &lt;/div&gt;
        &lt;form action="/save-department" method="POST"&gt;
          {{   csrf_field() }}
          &lt;div class="modal-body"&gt;         
            &lt;div class="form-group"&gt;
              &lt;label for="recipient-name" class="col-form-label"&gt;Name&lt;/label&gt;
              &lt;input type="text" class="form-control" name="name" id="recipient-name"&gt;
            &lt;/div&gt;
            &lt;div class="form-group"&gt;
              &lt;label for="recipient-name" class="col-form-label"&gt;ID&lt;/label&gt;
              &lt;input type="text" class="form-control" name="id" id="recipient-id"&gt;
            &lt;/div&gt; 
            &lt;div class="form-group"&gt;
              &lt;label for="recipient-name exampleFormControlSelect2" class="col-form-label"&gt;Parent ID&lt;/label&gt;
              &lt;input type="text" class="form-control" placeholder="Choose the department parent id"name="parent" id="recipient-parent"&gt;
  
              &lt;select multiple class="form-control"  name="parent" id="recipient-parent"&gt;
              @foreach($departments as $department)
                 &lt;option&gt;{{ $department-&gt;parent }}&lt;/option&gt;
                 @endforeach 
              &lt;/select&gt;
  
            &lt;/div&gt;          
          &lt;/div&gt;
          &lt;div class="modal-footer"&gt;
            &lt;button type="button" class="btn btn-secondary" data-dismiss="modal"&gt;Close&lt;/button&gt;
            &lt;button type="submit" class="btn btn-primary"&gt;Save&lt;/button&gt;
          &lt;/div&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="row"&gt;
    &lt;div class="col-md-12"&gt;
      &lt;div class="card"&gt;
        @if (session('status'))
        &lt;div class="alert alert-success" role="alert"&gt;
          {{ session('status') }}
        &lt;/div&gt;
        @endif
        &lt;div class="card-header"&gt;
          &lt;h4 class="card-title my-4 text-center font-weight-light"&gt; Department Management &lt;/h4&gt;
        &lt;/div&gt;
        &lt;div class="card-body"&gt;
          &lt;div class="table-responsive"&gt;
            &lt;table class="table"&gt;
              &lt;thead class=" text-primary"&gt;
                &lt;th&gt;Department&lt;/th&gt;
                &lt;button type="button" class="btn btn-primary float-right" data-toggle="modal" data-target="#exampleModal" &gt;Add&lt;/button&gt;              
                &lt;thead class=" text-primary"&gt;
                  &lt;th&gt;ID&lt;/th&gt;
                  &lt;th&gt;Name&lt;/th&gt;
                  &lt;th&gt;Parent ID&lt;/th&gt;
                  &lt;th&gt;Edit&lt;/th&gt;
                  &lt;th&gt;Delete&lt;/th&gt;
                &lt;/thead&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                @foreach($departments as $department)
                &lt;tr&gt;
                  &lt;td&gt;{{ $department-&gt;id }}&lt;/td&gt;  
                  &lt;td&gt;{{ $department-&gt;name }}&lt;/td&gt;  
                  &lt;td&gt;{{ $department-&gt;parent }}&lt;/td&gt; 
                  &lt;td&gt;
                    &lt;a href="{{  url('department-edit/'.$department-&gt;id) }}" class="btn btn-success"&gt;Edit&lt;/a&gt;
                  &lt;/td&gt;       
                  &lt;td&gt;
                    &lt;form action="{{  url('department-delete/'.$department-&gt;id) }}" method="POST"&gt;
                      {{ csrf_field() }}
                      {{ method_field('DELETE') }}
                      &lt;button type="submit"  class="btn btn-danger"&gt;Delete&lt;/button&gt;
                    &lt;/form&gt;                           
                  &lt;/td&gt;               
                &lt;/tr&gt; 
                @endforeach  
                &lt;a href="{{  url('/page') }}" class="btn btn-success"&gt;See the Departments and Employees&lt;/a&gt;                                                     
              &lt;/tbody&gt;           
            &lt;/table&gt;  
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
  
    @endsection
  
  
    @section('scripts')
  
  
    @endsection
  </code></pre>
  
  <p>web.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  use App\User;
  use App\Department;
  
  use App\Events\WebsocketDemoEvent;
  /*
  |--------------------------------------------------------------------------
  | 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('/', function () {
      broadcast(new WebsocketDemoEvent('some data'));
      return view('welcome');
  });
  Route::get('/page', function () {
      return view('admin.page');
  });
  Auth::routes();
  
  Route::get('/home', 'HomeController@index')-&gt;name('home');
  Route::group(['middleware' =&gt; ['auth','admin']], function  () {
      Route::get('/role-register','Admin\DashboardController@registered');
      Route::delete('/role-delete/{id}', 'Admin\DashboardController@registerdelete');//delete user
      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');//edit department
      Route::put('/department-update/{id}', 'Admin\DepartmentController@update');
      Route::delete('/department-delete/{id}', 'Admin\DepartmentController@delete');//delete department  
  
      Route::get('/page-users/{id}', 'Admin\DepartmentController@usersdep');//show users  
  
  
  });
  Auth::routes();
  Route::get('/home', 'HomeController@index')-&gt;name('home');
  
  Route::get('/chats', 'ChatsController@index');//chats
  Route::get('/messages', 'ChatsController@fetchMessages');//messages
  Route::post('/messages', 'ChatsController@sendMessage');//messages
  
  Route::get('/dashboard', 'Admin\DashboardController@dbcheck');//DATABASE
  Route::get('/user-edit/{id}', 'HomeController@registeredit');
  Route::get('/role-edit/{id}', 'Admin\DashboardController@registeredit');//edit user
  Route::put('/role-register-update/{id}', 'Admin\DashboardController@registerupdate');
  
  Auth::routes();
  Route::get('/home', 'HomeController@index')-&gt;name('home');
  
  
  Route::get('store_image', 'StoreImageController@index');
  Route::post('store_image/insert_image', 'StoreImageController@insert_image');
  Route::get('store_image/fetch_image/{id}', 'StoreImageController@fetch_image');
  Route::get('/page',array('as'=&gt;'jquery.treeview','uses'=&gt;'Admin\DepartmentController@treeView'));
  Route::get('/pageusers', 'Admin\DepartmentController@usersdep');
  </code></pre>
  
  <p>Department.php</p>
  
  <pre><code>&lt;?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-&gt;belongsTo(User::class);
      return $this-&gt;hasMany(User::class,'department','id');
  }
  
     //category has childs
     public function childs() {
      return $this-&gt;hasMany('App\Department','parent','id') ;
      /*
      childs() method with hasMany relationship. 
      hasMany relationship in Laravel tell us that they have multiple childs. 
      Here I am creating relationship based on parent and each category has their parent if parent id is 0 it means it is root category.*/
  }
  }
  
  </code></pre>
  
  <p>create_departments_table</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Migrations\Migration;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Support\Facades\Schema;
  
  class CreateDepartmentsTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
  public function up()
      {
          Schema::create('departments', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;string('name');
              $table-&gt;integer('parent');
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('departments');
      }
  }
  
  </code></pre>