【发布时间】:2016-03-05 20:21:08
【问题描述】:
我想根据哪个用户是 Auth 来更改表名。
为什么?因为当我添加一个dealer时,我为这个dealer创建了一个数据库客户端,数据的名字是d.$dealer_id.clients。因此,用户需要将客户添加到与自己的经销商关联的表中。
我试过 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'];
}
但它不会将客户端保存到表中。 还有这个:
'diclients'
应该是这样的:
'd'.Auth::user()->dealer_id.'clients'
这个东西我也试过了:
$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'];
文档说setTable应该可以工作,但我不知道我做错了什么......
【问题讨论】:
-
这个
$this->table = 'd1clients';应该是$this->table = $table;吗?然后运行setTable($dealer_id); -
您遇到了什么错误?你能看看 eloquent 的查询吗?
-
我正在尝试查找此查询。但我不知道在哪里可以找到它。
-
有问题,我检索了查询,唯一破坏这一切的部分是:
SQL: insert into "clients"。所以 setTable() 根本不起作用......它仍然是主表名......
标签: php database model laravel-5.1