0
votes

I have a vehicle database with a many to many relation with my variant and country table. I only want to show the title that's in my pivot (countries_variants) table. But when I set the relation in my api resource file, it shows all the columns from variants, countries_variants and countries table. Is there a way to only get the title from the pivot table?

Here is my Variant Model:

public function countries(): BelongsToMany
{
   return $this->belongsToMany(Country::class, 'countries_variants')->withPivot('title');
}                

VariantResource.php

public function toArray($request)
{
  return [
      'title' => $this->countries->first->title,
  ];
}

VariantController.php

public function index()
{
   return new VariantCollection(Variant::paginate(50));
}

The output I'm getting is:

{
 "data": [
    {
      "title": {
        "id": 1,
        "title": "Nederland",
        "country_code": "NL",
        "language_code": "NL_nl",
        "pivot": {
          "variant_id": 1,
          "country_id": 1,
          "title": "3/5 deurs"
        }
      }
    }
  ]
}

I just want to show "title": "3/5 deurs" and not the other data. I thought that if I set withPivot('title') in my model, it will only show that title and not the foreign keys (variant_id and country_id). Apparently thought wrong..

I tried adding this as well:

'variant_title' => $this->whenPivotLoaded('countries_variants', function () {
  return $this->pivot->title;
}),

But the data then returns empty.

Any help would be very much appreciated :)

2

2 Answers

0
votes

Try changing

$this->countries->first->title

To

$this->countries->first->title->pivot->title

When you do withPivot('title') you are telling Eloquent to also get title column, if you do not do that, you will only get the keys (variant_id and country_id in your case).

More info here.

0
votes

Okay so I finally figured it out. I looked through the official laravel docs again and I found this.

So I changed my CountryVariant model class to extend Pivot instead of Model.

Like so:

use Illuminate\Database\Eloquent\Relations\Pivot;

class CountryVariant extends Pivot {
    public function variant(): BelongsTo
    {
        return $this->belongsTo(Variant::class);
    }
}

Now in my VariantResource.php I added this:

public function toArray($request): array
{
   $items = [];

   foreach ($this->countries()->get() as $country) {
       $items[$country->language_code] = $country->pivot->only('title');
   }

   return $items;
}

I also changed the pivot table names to be singular like: country_variant (I had it plural).

Apparently the problem was that in my model it was looking for a primary key which I didn't have in my pivot table. I only had foreign keys and an additional column: 'title'. It took one of my foreign keys and used it as a primary one. So when I extended with extends pivot it would ignore the primary key and I could collect my alternate column 'title' by using country->pivot->only('title') in my resource file.

I hope this helps some one else.