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

如何投射 Eloquent Pivot 參數?

How To Cast Eloquent Pivot Parameters?(如何投射 Eloquent Pivot 參數?)
本文介紹了如何投射 Eloquent Pivot 參數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下帶有關系的 Eloquent 模型:

I have the following Eloquent Models with relationships:

class Lead extends Model 
{
    public function contacts() 
    {
        return $this->belongsToMany('AppContact')
                    ->withPivot('is_primary');
    }
}

class Contact extends Model 
{
    public function leads() 
    {
        return $this->belongsToMany('AppLead')
                    ->withPivot('is_primary');
    }
}

數據透視表包含一個附加參數 (is_primary),用于將關系標記為主要關系.目前,我在查詢聯系人時看到這樣的返回:

The pivot table contains an additional param (is_primary) that marks a relationship as the primary. Currently, I see returns like this when I query for a contact:

{
    "id": 565,
    "leads": [
        {
            "id": 349,
             "pivot": {
                "contact_id": "565",
                "lead_id": "349",
                "is_primary": "0"
             }
        }
    ]
}

有沒有辦法將其中的 is_primary 轉換為布爾值?我已經嘗試將它添加到兩個模型的 $casts 數組中,但這并沒有改變任何東西.

Is there a way to cast the is_primary in that to a boolean? I've tried adding it to the $casts array of both models but that did not change anything.

推薦答案

由于這是數據透視表上的一個屬性,因此使用 $casts 屬性將不適用于 LeadContact 模型.

Since this is an attribute on the pivot table, using the $casts attribute won't work on either the Lead or Contact model.

但是,您可以嘗試的一件事是使用自定義 Pivot 模型并定義了 $casts 屬性.自定義數據透視模型的文檔位于此處.基本上,您使用自定義創建一個新的 Pivot 模型,然后更新 LeadContact 模型以使用此自定義 Pivot 模型而不是基礎模型.

One thing you can try, however, is to use a custom Pivot model with the $casts attribute defined. Documentation on custom pivot models is here. Basically, you create a new Pivot model with your customizations, and then update the Lead and the Contact models to use this custom Pivot model instead of the base one.

首先,創建您的自定義 Pivot 模型,它擴展了基本的 Pivot 模型:

First, create your custom Pivot model which extends the base Pivot model:

<?php namespace App;

use IlluminateDatabaseEloquentRelationsPivot;

class PrimaryPivot extends Pivot {
    protected $casts = ['is_primary' => 'boolean'];
}

現在,覆蓋 LeadContact 模型上的 newPivot() 方法:

Now, override the newPivot() method on the Lead and the Contact models:

class Lead extends Model {
    public function newPivot(Model $parent, array $attributes, $table, $exists) {
        return new AppPrimaryPivot($parent, $attributes, $table, $exists);
    }
}

class Contact extends Model {
    public function newPivot(Model $parent, array $attributes, $table, $exists) {
        return new AppPrimaryPivot($parent, $attributes, $table, $exists);
    }
}

這篇關于如何投射 Eloquent Pivot 參數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 | 一区二区三区四区在线 | 亚洲精品乱码久久久久久黑人 | 黄色片a级 | 91精品亚洲 | 久久久久一区二区 | 日本大片在线播放 | 亚洲午夜精品 | 神马福利 | 国产高清在线 | 欧美一级淫片免费视频黄 | 在线中文视频 | 日韩a在线| 一级h片| 精品国产一区二区三区性色av | 欧美成人免费在线 | 欧美一区二区三区在线 | 国产一区日韩在线 | 中文字幕 在线观看 | 日本韩国电影免费观看 | 亚洲国产高清高潮精品美女 | 久久这里有精品 | 成人免费视频网址 | 成人一区在线观看 | 国产精品久久久久一区二区三区 | 人人干人人干人人 | www.一区二区 | 国产精品av久久久久久久久久 | 国产成人综合在线 | 国产日韩欧美中文 | 欧美日韩在线综合 | 久久久精品影院 | 夜夜久久| 日韩欧美中文字幕在线观看 | 男女av| 亚洲精品大片 | 国产精品久久久久久久久久 | 自拍视频在线观看 | 夜夜爽99久久国产综合精品女不卡 | 亚洲精品高清视频 | 九九国产 |