mardi 1 décembre 2015

Laravel Model's table name with variable

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...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire