久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Eloquent 模型中動態設置表名

How to dynamically set table name in Eloquent Model(如何在 Eloquent 模型中動態設置表名)
本文介紹了如何在 Eloquent 模型中動態設置表名的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 Laravel 的新手.我正在嘗試使用 Eloquent 模型訪問數據庫中的數據.

I am new to Laravel. I am trying to use Eloquent Model to access data in DB.

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

I have tables that shares similarities such as table name.

所以我想使用一個模型來訪問數據庫中的多個表,如下所示,但沒有運氣.

So I want to use one Model to access several tables in DB like below but without luck.

有沒有辦法動態設置表名?

Is there any way to set table name dynamically?

任何建議或建議將不勝感激.提前致謝.

Any suggestion or advice would be appreciated. Thank you in advance.

型號:

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 建議的方式更改表名.

I was able to change table name by the way @Mahdi Younesi suggested.

我可以通過如下方式添加 where 條件

And I was able to add where conditions by like below

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

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

推薦答案

以下 trait 允許在 hydration 期間傳遞表名.

The following trait allows for passing on the table name during hydration.

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;
}

像這樣在實例上調用方法:

Call the method on instance like this:

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

   $productLog->setTable('anotherTableName');

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


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

這篇關于如何在 Eloquent 模型中動態設置表名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨立數據))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創建自定義輔助函數的最佳實踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
主站蜘蛛池模板: 国产精久久久久久久 | 欧美精品在线免费观看 | 超碰精品在线 | 亚洲综合五月天婷婷 | 国产一区二区三区四区 | 范冰冰一级做a爰片久久毛片 | 一区二区三区视频在线观看 | 97超碰成人 | 亚洲精品大片 | 国产精品永久免费视频 | 欧美在线观看一区 | 黄色成人亚洲 | 亚洲 日本 欧美 中文幕 | 青青草社区 | av性色全交蜜桃成熟时 | 亚洲一区在线观看视频 | 久久久成| 国产永久免费 | 在线一区观看 | 久久91 | 视频在线一区二区 | 亚洲精品久久久久久一区二区 | 毛片a级毛片免费播放100 | 国产免费拔擦拔擦8x高清 | 九九av| 日韩成人影院 | 天天干精品 | 日本精品一区二区三区视频 | 精品国产18久久久久久二百 | 精品日韩一区 | 97伦理| 97伦理电影 | 日韩在线不卡 | 免费观看www| 国产成人在线一区二区 | av电影手机在线看 | 午夜小视频免费观看 | 97国产一区二区精品久久呦 | 亚洲一区二区三区视频 | 99免费在线| 国产精品久久久久aaaa九色 |