title: Casting varchar to date in where query Laravel 5.2
body: <p>I am trying to pick a collection of data from a range using the date as the sorting column. But the problem is that the date in my table is saved as a varchar string. </p>
  
  <p>NOTE: The varchar field type needs to stay as it used by some non laravel, legacy codebase, and I will disrupt service if altered to date type. </p>
  
  <p>I have tried casting it but it doesn't work. </p>
  
  <p>This is the code in the controller</p>
  
  <pre><code>        switch ($range) {
          case 'thisWeek':
              $start = Carbon::now()-&gt;startOfWeek()-&gt;format('d-m-y');
              $end = Carbon::now()-&gt;endOfWeek()-&gt;format('d-m-Y');     
              break;
  
          case 'thisMonth':
              $start = Carbon::now()-&gt;startOfMonth()-&gt;format('d-m-Y');
              $end = Carbon::now()-&gt;endOfMonth()-&gt;format('d-m-Y');
              // dd($start, $end);
              break;
  
  
          case 'last90Days':
              $start = Carbon::now()-&gt;format('d-m-Y');
              $end = Carbon::now()-&gt;subDays(90)-&gt;format('d-m-Y');
  
              // dd($start, $end);
              break;
  
          case 'last30Days':
              $start = Carbon::now()-&gt;format('d-m-Y');
              $end = Carbon::now()-&gt;subDays(30)-&gt;format('d-m-Y');
              break;
  
  
          case 'year':
              $start = Carbon::now()-&gt;format('d-m-Y');
              $end = Carbon::now()-&gt;year;
              $end = "31-12-". $end;
              break;
  
          // case 'all':
          //  $route = $name .".". "index";
          //  return redirect()-&gt;Route($route);
  
          default:
              session()-&gt;flash("alert-danger", "no range selected");
              return redirect()-&gt;back();
              break;
      }
  
      if($name=="activitylog"){
  
          // return $start . ' &lt;br&gt;' . $end;
  
          $logs = Payment::where("payDate", '&lt;', $start)-&gt;where("payDate", "&gt;", $end)-&gt;
                          orderBy("payDate", "DESC")-&gt;
                          paginate(20);
      }
      else{
          $logs = Payment::where("description", $description[$name])-&gt;
                      where("payDate", '=&lt;', $start)-&gt;where("payDate", "&gt;=", $end)-&gt;
                      orderBy("payDate", "DESC")-&gt;
                      paginate(20);   
  </code></pre>
  
  <p>the model:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Models\Admin;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Payment extends Model
  {
      protected $table = "payments";
      public $timestamps = false;
      protected $cast = ["payDate"=&gt; "DATE"];
  }
  </code></pre>
  
  <p>When the user makes a request for any of the ranges I wish to compare and give an answer. </p>
  
  <p><a href="https://i.stack.imgur.com/koJrI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/koJrI.png" alt="dates example"></a></p>
  
  <p>the image for the database config is above
  and the structure below
  <a href="https://i.stack.imgur.com/j6nkg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j6nkg.png" alt="table structure"></a></p>