title: How to dynamically set table name in Eloquent Model
body: <p>I am new to Laravel. I am trying to use Eloquent Model to access data in DB.</p>
  
  <p>I have tables that shares similarities such as table name.</p>
  
  <p>So I want to use one Model to access several tables in DB like below but without luck.</p>
  
  <p>Is there any way to set table name dynamically?</p>
  
  <p>Any suggestion or advice would be appreciated. Thank you in advance.</p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>class ProductLog extends Model
  {
  
      public $timestamps = false;
  
      public function __construct($type = null) {
  
          parent::__construct();
  
          $this-&gt;setTable($type);
      }
  }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public function index($type, $id) {
  
      $productLog = new ProductLog($type);
  
      $contents = $productLog::all();
  
      return response($contents, 200);
  }
  </code></pre>
  
  <p><strong>Solution For those who suffer from same problem:</strong></p>
  
  <p>I was able to change table name by the way @Mahdi Younesi suggested.</p>
  
  <p>And I was able to add where conditions by like below</p>
  
  <pre><code>$productLog = new ProductLog;
  $productLog-&gt;setTable('LogEmail');
  
  $logInstance = $productLog-&gt;where('origin_id', $carrier_id)
                            -&gt;where('origin_type', 2);
  </code></pre>