久久久久久久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 重定向到登錄頁面)
主站蜘蛛池模板: 日韩中文字幕 | 国产电影一区 | 亚洲成人av在线播放 | 99久久精品国产一区二区三区 | 久久精品国产一区老色匹 | 欧美一级特黄aaa大片在线观看 | 欧美日韩在线一区二区三区 | 九九热视频这里只有精品 | 暖暖成人免费视频 | 黄色一级毛片 | 欧美一级久久 | 亚洲精品一区二区三区在线 | 天堂久久一区 | 国产视频久久 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 国产在线网站 | 亚洲精品中文字幕av | 午夜网站视频 | 精品一区二区视频 | 国产精品久久二区 | 国产在线高清 | 狠狠插狠狠操 | 中国大陆高清aⅴ毛片 | 国产一区二区在线免费观看 | 色婷婷在线视频 | 91精品国产91久久久久久 | 久草网站 | 在线资源视频 | 欧美日本在线观看 | 日韩精品免费一区二区在线观看 | 欧美福利在线 | 一区二区手机在线 | 亚洲欧美中文日韩在线 | 日本久久久久久久久 | 激情综合五月 | 国产亚洲网站 | 久久久青草婷婷精品综合日韩 | 国产一区二区 | 亚洲精品片 | 91青娱乐在线 | 成人h视频在线观看 |