mercredi 2 décembre 2015

Laravel 5.1 Eloquent change table on Model::find

In a previous question, I was looking for save query using a Model and change the table when I add a new query. I found my answer with some tips by you. Now, when I update a row, I'm unable to retrieve this row because the Model::find use the tablename set in the model. I tried to use this:

$client = Client::fin(something);
$client -> setTable(newTableName);
$client -> things to update...
$client -> save();

This doesn't work. I tried some variants like this:

$client = Client->setTable(newTableName)::find(something);

But it doesn't work too... And I tried some other things.

Now, I'm asking for your help to find the issue.

What I know, when you use Model::find, it call a function like this:

public function getQualifiedKeyName()
{
    return $this->getTable().'.'.$this->getKeyName();
}

public function getKeyName()
{
    return $this->primaryKey;
}

But I am not really sure how I can modify this to input some variable in the get table name to find the row in the right table.

The table associate with the model Client is clients but it should be:

'd'.Auth::user()->dealer_id.'clients'

So if the user is associate with Dealer ABC and the id of this dealer is 1, the tablename will be d1clients.

Can you show me the right direction?

Thank you all!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire