iT邦幫忙

0

有關laravel 的relationship的問題

大家好,

假設我想在item table的itemCustID, 是這樣5,4,17這個string,每個數字就是獨立的一個customizationtype id

之後想在customizationtype 這個table 裡根據5,4,17這些ID, 拿到customization Table
裡的related records.

怎樣可以用relationship 寫到?

還是用laravel 的database Query 就算?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
Victor
iT邦新手 2 級 ‧ 2020-01-17 14:30:12

如果資料庫不能更動結構,App\Item Model 新增以下方法

public function customizationtypes()
{
    $itemIds = explode(',', $this->itemCustID);
    $results = App\Customizationtype::whereIn(id, $itemIds)->get();
    return $results;
}

要取得關聯資料實 $item->customizationtypes();

試試看!沒有實際測試過!

我要發表回答

立即登入回答