簡體   English   中英

從與單列分隔的逗號中檢索數據的方法是什么?

[英]what's the way to retrieve data from comma separated from s single column?

在此處輸入圖像描述

我正在嘗試從 product_category 中檢索產品表中的貓 ID。

controller.php

$shop['product'] = Product::with('ProductImages')->where('product_status','=',1)->where('product_drop_status','=','no')->where('language_code','=',$translate)->whereRaw('find_in_set(cat-,product.product_category)')->orderBy('product_id','desc')->get();

我需要檢索值“19”。 controller 有什么問題?

在您的產品 model 上,您可以設置一個屬性:

public function setCatIdAttribute(){
    $chunks = explode(',', $this->product_category);
    foreach($chunks as $chunk){
        if (strpos($chunk, 'cat-') !== false) {
            return str_replace('cat-', '', $chunk);
        }
    }
    return null;
}

在您的收藏中,您可以像這樣訪問:

foreach($products as $product){
    echo $product->cat_id;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM