I want to change the table name depending on which user is Auth.
Why? Because when I add a dealer, I create a database client for this dealer and the name of the data is d.$dealer_id.clients. So, the user need to add a client to the table associated with is own dealer.
I tried with setTable() :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
class Client extends Model
{
    public function setTable($table)
    {
        $this->table = 'd1clients';
        return $this;
    }
    protected $fillable = ['dealer_id', 'user_id', 'type', 'first_name', 'last_name', 'phone', 'cellphone', 'email', 'stock', 'source', 'civic', 'road', 'city', 'province', 'country', 'postal_code', 'birth_date', 'driving_liscence'];
}
But it don't save the client to the table. And this:
'diclients'
Should be this:
'd'.Auth::user()->dealer_id.'clients'
I also tried this thing:
$globalDealerId = Auth::user()->dealer_id;
protected $table = 'd'.$globalDealerId.'clients';
protected $fillable = ['dealer_id', 'user_id', 'type', 'first_name', 'last_name', 'phone', 'cellphone', 'email', 'stock', 'source', 'civic', 'road', 'city', 'province', 'country', 'postal_code', 'birth_date', 'driving_liscence'];
The documentation said that the setTable should work, but I don't know what I did wrong...
I found the issue. There is how it work:
The model need have a function in it:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
    public function setTable($table)
    {
        $this->table = $table;
        return $this;
    }
    protected $fillable = ['dealer_id', 'user_id', 'type', 'first_name', 'last_name', 'phone', 'cellphone', 'email', 'stock', 'source', 'civic', 'road', 'city', 'province', 'country', 'postal_code', 'birth_date', 'driving_liscence'];
}
The important part is this one :
public function setTable($table)
{
    $this->table = $table;
    return $this;
}
Then, I go where I add the client, in this case, the client is added to the database within the controller. So there you go and you do this:
public function store(Request $request)
{
    $client =  new Client;
    $client -> setTable('d'.Auth::user()->id.'clients');
    $client -> dealer_id    = Auth::user()->dealer_id;
    $client -> user_id      = Auth::user()->id;
    $client -> type         = Request::get('type');
    $client -> first_name   = Request::get('first_name');
    $client -> last_name    = Request::get('last_name');
    $client -> phone        = Request::get('phone');
    $client -> cellphone    = Request::get('cellphone');
    $client -> email        = Request::get('email');
    $client -> city         = Request::get('city');
    $client -> stock        = Request::get('stock');
    $client -> source       = Request::get('source');
    $client -> save();
The important line is this one:
$client -> setTable('d'.Auth::user()->id.'clients');
Don't forget the namespace at the begining:
use DB;
Now, the user A who belong to company A will add clients to daclients table. The user B from company B will add clients to dbclients table.
Special thanks to @amir bar that helped me with the query logs. And thanks for this question : model - Update the table name at runtime not working - laravel Eloquent ORM
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With