1

I have two tables that are connected with a pivot table.

shows:

+----+--------+
| id | show   |
+----+--------+
|  1 | show1  |
|  2 | show2  |
|  3 | show3  |
+----+--------+

draft_types:

+----+--------+
| id | type   |
+----+--------+
|  1 | type1  |
|  2 | type2  |
|  3 | type3  |
+----+--------+

show_draft_types:

+---------+---------------+
| show_id | draft_type_id |
+---------+---------------+
|  1      | 1             |
|  1      | 2             |
|  1      | 3             |
+---------+---------------+

So essentially a show has many different draft types and here's the relationship that I'm using in eloquent for shows:

public function draft_types()
{
    return $this->belongsToMany('App\Models\DraftTypes', 'show_draft_types', 'show_id', 'draft_type_id');
}

The problem is whenever I do:

Shows::where('id', 1)->with(['draft_types'])->first()

I get this:

{
    id: 1,
    show: "show1",
    draft_types: [
        {
            id: 1,
            type: "type1"
        },
        {
            id: 2,
            type: "type2"
        },
        {
            id: 3,
            type: "type3"
        }
    ]
}

What I want to get is this:

{
    id: 1,
    show: "show1",
    draft_types: [
        "type1",
        "type2",
        "type3"
    ]
}

Is there a way to do this with eloquent relationships?

1
  • 1
    There's a somewhat convoluted way you could do this with the protected $appends property of your Show model, but if this is a simple "one-off", you can simply append it manually, like $show = Show::with('draft_types')->find(1);, then $show->draft_types = $show->draftTypes->pluck('type'); (sidenote, method name should be draftTypes() to follow proper method naming convention) Commented May 22, 2024 at 17:12

1 Answer 1

2

You could add an appended attribute to the Show model and an accessor for the desired array:

// Show.php

class Show extends Model
{
    protected $appends = ['draft_types_array'];

    public function draft_types()
    {
        return $this->belongsToMany('App\Models\DraftTypes', 'show_draft_types', 'show_id', 'draft_type_id');
    }

    public function getDraftTypesArrayAttribute()
    {
        return $this->draftTypes->pluck('type')->toArray();
    }
}

Usage:


$show = Show::where('id', 1)->with(['draft_types'])->first();

dd($show->draft_types_array);


// Outputs: 
// [
//   "type1",
//   "type2",
//   "type3"
//  ]

Sign up to request clarification or add additional context in comments.

2 Comments

Just a heads-up, while this wasn't explicitly tagged laravel-11, it's better to assume questions are referencing the latest version of technologies, and the syntax for Attributes has changed: laravel.com/docs/11.x/eloquent-mutators#defining-an-accessor
Also, the model is Show, not Shows 🙂

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.