国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

在結(jié)果中為相同名稱的Laravel關(guān)係重新命名
P粉163951336
P粉163951336 2023-12-15 09:40:36
0
2
773

在這裡,你可以看到我有兩個(gè) car_model 主表欄位和關(guān)係名稱:

AppModelsProduct {#1478 ▼
  #connection: "mysql"
  #table: "products"
  ...
  #escapeWhenCastingToString: false
  #attributes: array:21 [▼
    "id" => 1
    "company_id" => 1
    ...
    "car_model" => "test"
    ...
  ]
  #original: array:21 [?]
  ...
  #relations: array:5 [▼
    "company" => AppModelsCompany {#1506 ?}
    "car_model" => AppModelsCarModel {#1508 ▼
      #connection: "mysql"
      #table: "car_models"
      #attributes: array:6 [▼
        "id" => 1
        "title" => "test"
        "created_at" => ":07:25"
        "updated_at" => ":07:58"
      ]
      ...
      +mediaConversions: []
      +mediaCollections: []
      #deletePreservingMedia: false
      #unAttachedMediaLibraryItems: []
    }
    ...
}

當(dāng)我嘗試取得 car_model 相關(guān)關(guān)係並同時(shí)擁有 car_model 時(shí),我該如何取得關(guān)聯(lián)式資料?例如:

$products->first()->car_model->title

產(chǎn)品型號(hào):

    public function car_model(): BelongsTo
    {
        return $this->belongsTo(CarModel::class);
    }

和我的查詢:

$this->products = Product::with(
    [
        'car_model',
    ]
)->get();

P粉163951336
P粉163951336

全部回覆(2)
P粉555682718

我建議您將關(guān)係重新命名為 car_models 或 car_model 之外的其他名稱:

public function car_models(): BelongsTo
    {
        return $this->belongsTo(CarModel::class);
    }

並且查詢可以更改為這樣

$this->products = Product::with(
    [
        'car_models',
    ]
)->get();

然後回傳

$products->first()->car_models->title
P粉145543872

我找到了解決方案。將 object 轉(zhuǎn)換為 array 後,我可以將 car_model 作為 relationship 存?。?/p>

$i=$products->first()->toArray();
echo $i['car_model']['title'];

echo ($products->first()->toArray())['car_model']['title']
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板