首頁 > 資料庫 > mysql教程 > 如何從 Laravel 中的資料透視表列檢索價格值?

如何從 Laravel 中的資料透視表列檢索價格值?

Susan Sarandon
發布: 2024-11-10 07:53:02
原創
1085 人瀏覽過

How to Retrieve the Price Value from a Pivot Table Column in Laravel?

在Laravel 中擷取額外資料透視表列的價格值

在Laravel 中,當透過Eloquent 利用多對多關係時,可以存取資料透視表中的其他列被簡化。這些額外的列可以提供連結相關模型的有價值的資訊。

要啟用對這些列的訪問,必須在定義關係時明確指定它們:

return $this->belongsToMany('Role')->withPivot('foo', 'bar');
登入後複製

這可確保生成的模型的資料透視屬性將包含指定的列。

在提供的場景中,目標是使用其 slugs 來檢索特定phone_model 和phone_problem 的價格值。建議採用以下方法:

$model = $phoneService->getModelFromSlug($model_slug);
$problem = $phoneService->getProblemFromSlug($problem_slug);

$price = $model
    ->problems()
    ->where('phone_problem_id', $problem->id)
    ->first()
    ->pivot->price;
登入後複製

以上是如何從 Laravel 中的資料透視表列檢索價格值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板