title: Laravel 5.3 Eloquent Model Dynamic Table Property
body: <p>Thanks for your time in advance. Skip to the part in bold if you're in a hurry.</p>
  
  <p>I have a following use case: every user of the app uses his own set of prefixed tables for Eloquent Models (i.e. Invoice.php for code_invoices and Client.php for code_clients). The prefixes are defined in users table (users.code for example).</p>
  
  <p>In Laravel 5.2 I've been able to easily set the $table property of the Eloquent model by reading the config (or session) variable set at runtime after user logs in. Everything worked fine, including the relations.</p>
  
  <p>However after the 5.3 update any config value set at runtime now returns null in the object (as does the session value). I'm aware from the docs that session is not available in the constructor now, and the suggested workaround is the middleware closure, but Eloquent classes cannot use the middleware closure, as it returns an undefined method error.</p>
  
  <p><strong>So my question is two-fold: how do I dynamically set the Eloquent's $table property and / or how do I read the runtime config values in the model.</strong></p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>EDIT:
  I'm able to specify a table for the model with the <code>from()</code> method. 
  This works: 
  <code>$model = \App\ModelName::from('prefix_table')-&gt;first();</code>
  But my real problem is relations.
  This does not work: 
  <code>$model = \App\ModelName::from('prefix_table')-&gt;with('relatedModel')-&gt;first()</code> does not, because the related model doesn't know what table prefix to use.</p>
  
  <p>Interesting, huh? :)</p>