How To Cast Eloquent Pivot Parameters

0 votes

I have the following Eloquent Models with relationships:

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

class Contact extends Model 
{
    public function leads() 
    {
        return $this->belongsToMany('App\Lead')
                    ->withPivot('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 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.

Nov 12, 2020 in Laravel by kartik
• 37,510 points
2,620 views

1 answer to this question.

0 votes

Hello,

In Laravel 5.1 or higher you can use dot notation for pivot casts:

protected $casts = [
    'id' => 'integer',
    'courses.pivot.course_id' => 'integer',
    'courses.pivot.active' => 'boolean'
]

Hope it helps!!

answered Nov 12, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How to exclude certains columns while using eloquent?

Hello @kartik, Using hidden array in model is good, but ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,880 points
13,876 views
0 votes
1 answer

How to manually create a new empty Eloquent Collection in Laravel?

Hello @kartik, It's not really Eloquent, to add ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,880 points
12,213 views
0 votes
1 answer

How to get current path of a Request with its query parameters?

Hello @kartik, Try to use the following: \Request::getRequestUri() Hope this ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,880 points
1,701 views
0 votes
1 answer

How To Pass GET Parameters To Laravel From With GET Method ?

Hello @kartik, The simplest way is just to ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,880 points
9,555 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,750 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,647 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,504 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,364 views
0 votes
1 answer

How to update a pivot table using Eloquent in laravel 5?

Hello @kartik, The code below solved my problem: $messages ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,880 points
4,043 views
0 votes
1 answer

How to Make Laravel Eloquent “IN” Query?

Hello @kartik, Here is how you do in ...READ MORE

answered Aug 4, 2020 in Laravel by Niroj
• 82,880 points
674 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP