Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在雄辩模型中动态设置表名_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 如何在雄辩模型中动态设置表名

Php 如何在雄辩模型中动态设置表名,php,laravel,laravel-5,Php,Laravel,Laravel 5,我是拉雷维尔的新手。我试图使用雄辩的模型来访问数据库中的数据 我有一些表具有相似性,例如表名 所以我想用一个模型访问数据库中的多个表,如下所示,但没有运气 有没有办法动态设置表名 如有任何建议,将不胜感激。先谢谢你 型号: class ProductLog extends Model { public $timestamps = false; public function __construct($type = null) { parent::__cons

我是拉雷维尔的新手。我试图使用雄辩的模型来访问数据库中的数据

我有一些表具有相似性,例如表名

所以我想用一个模型访问数据库中的多个表,如下所示,但没有运气

有没有办法动态设置表名

如有任何建议,将不胜感激。先谢谢你

型号:

class ProductLog extends Model
{

    public $timestamps = false;

    public function __construct($type = null) {

        parent::__construct();

        $this->setTable($type);
    }
}
public function index($type, $id) {

    $productLog = new ProductLog($type);

    $contents = $productLog::all();

    return response($contents, 200);
}
控制器:

class ProductLog extends Model
{

    public $timestamps = false;

    public function __construct($type = null) {

        parent::__construct();

        $this->setTable($type);
    }
}
public function index($type, $id) {

    $productLog = new ProductLog($type);

    $contents = $productLog::all();

    return response($contents, 200);
}
针对同样问题的解决方案:

class ProductLog extends Model
{

    public $timestamps = false;

    public function __construct($type = null) {

        parent::__construct();

        $this->setTable($type);
    }
}
public function index($type, $id) {

    $productLog = new ProductLog($type);

    $contents = $productLog::all();

    return response($contents, 200);
}
我可以按照@Mahdi Younesi的建议更改表名

我可以像下面这样添加where条件

$productLog = new ProductLog;
$productLog->setTable('LogEmail');

$logInstance = $productLog->where('origin_id', $carrier_id)
                          ->where('origin_type', 2);

以下特性允许在水合过程中传递表名

trait BindsDynamically
{
    protected $connection = null;
    protected $table = null;

    public function bind(string $connection, string $table)
    {
       $this->setConnection($connection);
       $this->setTable($table);
    }

    public function newInstance($attributes = [], $exists = false)
    {
       // Overridden in order to allow for late table binding.

       $model = parent::newInstance($attributes, $exists);
       $model->setTable($this->table);

       return $model;
    }

}
以下是如何使用它:

class ProductLog extends Model
{
   use BindsDynamically;
}
对实例调用方法,如下所示:

public function index() 
{
   $productLog = new ProductLog;

   $productLog->setTable('anotherTableName');

   $productLog->get(); // select * from anotherTableName


   $productLog->myTestProp = 'test';
   $productLog->save(); // now saves into anotherTableName
}

那么,您想每次传递不同的表名吗?是的。仅适用于某些表格;具有类似属性的表。我可以建议您为所有表创建模型,但我可以告诉您一个通用函数,该函数将通过仅向该函数传递模型名称来创建任何模型的对象,您将动态获取模型对象。如果这是绝对必要的,您可以重写
getTable
方法,但您可能可以使用查询范围并在其中设置表名。它会更改表名,但在我使用
$productLog->setTable('anotherTableName')之后,如果我要使用
$productLog->where('some query')
,则它不起作用。