title: How do I show the name of the related tables in my "empleados" datatable after doing a search in Laravel 8?
body: <p>Sorry for my grammar, english is not my native language.</p>
  <p>My data table is working fine when is not doing any search. But, when I do a search with the field <strong>nombre</strong> is giving me this error:</p>
  <p><code>Undefined property: stdClass::$departamento (View: /Users/xxx/Sites/contablerd/resources/views/rrhh/empleado/index.blade.php)</code></p>
  <p>This is my &quot;Advanced Search&quot; above the table:</p>
  <pre><code>&lt;form action=&quot;#&quot; method=&quot;GET&quot;&gt;
              &lt;h3&gt;Busqueda Avanzada&lt;/h3&gt;&lt;br&gt;
              &lt;div class=&quot;row&quot;&gt;
                      &lt;div class=&quot;row g-2&quot;&gt;
                          &lt;div class=&quot;col-md&quot;&gt;
                              &lt;div class=&quot;form-floating&quot;&gt;
                                  &lt;input type=&quot;nombre&quot; class=&quot;form-control&quot; id=&quot;nombre&quot; name=&quot;nombre&quot; placeholder=&quot;&quot; value=&quot;&quot;&gt;
                                  &lt;label for=&quot;nombre&quot;&gt;Nombre&lt;/label&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
              &lt;/div&gt;&lt;br&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Search&quot; class=&quot;btn btn-secondary&quot;&gt;
  &lt;/form&gt;
  </code></pre>
  <p>This is the part of the code where I am using a foreach. This part work fine when I am not using the &quot;Advanced Search&quot; (rrhh/empleado/index.blade.php):</p>
  <pre><code>@foreach($empleados as $empleado)
              &lt;tr&gt;
                  &lt;td&gt;{{$empleado-&gt;id}}&lt;/td&gt;
                  &lt;td&gt;{{$empleado-&gt;nombre}}&lt;/td&gt;
                  &lt;td&gt;{{$empleado-&gt;apellido}}&lt;/td&gt;
                  &lt;td&gt;{{$empleado-&gt;departamento-&gt;det}}&lt;/td&gt;
                  &lt;td&gt;{{$empleado-&gt;cargo-&gt;det}}&lt;/td&gt;
                  &lt;td&gt;{{$empleado-&gt;nomina-&gt;det}}&lt;/td&gt;
                  &lt;td&gt;{{number_format($empleado-&gt;sueldo,2)}}&lt;/td&gt;
                  &lt;td&gt;{{number_format($empleado-&gt;ars_empleado+$empleado-&gt;afp_empleado,2)}}&lt;/td&gt;
                  &lt;td&gt;{{number_format($empleado-&gt;sueldo-($empleado-&gt;ars_empleado-$empleado-&gt;afp_empleado),2)}}&lt;/td&gt;
                  &lt;td&gt;
                      &lt;a class=&quot;btn btn-info&quot; href=&quot;/empleado/{{$empleado-&gt;id}}/edit&quot;&gt;Editar&lt;/a&gt;
                      &lt;button class=&quot;btn btn-danger&quot;&gt;Borrar&lt;/button&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          @endforeach
  </code></pre>
  <p>This is my index method where I confirm is there is any request to change the query (<strong>EmpleadoController.php</strong>):</p>
  <pre><code>public function index(Request $request)
      {
          $empleados = \DB::table('empleados');
  
          if($request-&gt;input('nombre')){
              $empleados = $empleados-&gt;where('nombre', 'LIKE', &quot;%&quot;.request('nombre').&quot;%&quot;);
              $empleados = $empleados-&gt;get();
          }else{
              $empleados = Empleado::all();
          }
          return view('rrhh.empleado.index',
              [
                  'empleados'=&gt;$empleados
              ]);
      }
  </code></pre>
  <p>So when I do a search is giving me the above error. I got all my relationships in <strong>Empleado.php</strong> model.</p>
  <p>I would like to ask too, is this the correct way to do this?</p>
  <p>Thanks in advance and happy rest of the day.</p>