title: How to use substring with laravel eloquent?
body: <p>I have 2 related tables the problem is that in one that brings the clients of each user, but the client code is based mainly on 4 digits, the problem is that there are many users who have more than 4 digits 8 or 9 that this represents your task in the company. But I need to bring only the first 4 that I need to make a relationship with the table and be able to bring only the name of the company</p>
  
  <p>I'm dealing with <code>substring</code> in sql like this</p>
  
  <pre><code>$client = DB::connection('dpnmwin')-&gt;table('nmtrabajador')-&gt;select('SUBSTRING'('COD_UND', 1, 4))-&gt;where('CONDICION', '=', 'A')-&gt;get()
  </code></pre>
  
  <p>but this marks me error from the same code editor.</p>
  
  <p>How can I do it using substring or otherwise?</p>
  
  <p>Model Employee</p>
  
  <pre><code>class Employee extends Model
  {
      protected $connection = 'dpnmwin';
  
      protected $table = 'nmtrabajador';
  
      protected $primaryKey = 'CODIGO';
  
      public function client(){
  
          return $this-&gt;belongsTo('App\Client', 'COD_UND');
  
      }
  
      public function position(){
  
          return $this-&gt;belongsTo('App\Position', 'COD_CARGO');
  
      }
  
  }
  </code></pre>
  
  <p>Model Client</p>
  
  <pre><code>class Client extends Model
  {
      protected $connection = 'dpnmwin';
  
      protected $table = 'nmundfunc';
  
      protected $primaryKey = 'CEN_CODIGO';
  
      public function employee(){
  
          return $this-&gt;hasMany('App\Employee');
  
      }
  
  }
  </code></pre>
  
  <p>File list.blade.php</p>
  
  <pre><code> @foreach ($users as $user)
         &lt;tr&gt;
             &lt;td&gt;{{$user-&gt;CEDULA}}&lt;/td&gt;
             &lt;td&gt;{{$user-&gt;NOMBRE}}&lt;/td&gt;
             &lt;td&gt;{{$user-&gt;APELLIDO}}&lt;/td&gt;
             &lt;td&gt;{{$user-&gt;EMAIL}}&lt;/td&gt;
             &lt;td&gt;{{$user-&gt;position-&gt;CAR_DESCRI}}&lt;/td&gt;
             &lt;td&gt;{{$user-&gt;client-&gt;CEN_DESCRI}}&lt;/td&gt;
             &lt;td&gt;&lt;a href="{{ route('detailUser', ['user_id' =&gt; $user-&gt;CODIGO]) }}"&gt;&lt;i class="fas fa-user"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
   @endforeach
  </code></pre>