Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

You can hide pivot object from toJson() method on the related model and tweak the relationship definition to have desirec column visible on the main model:

// Category model
public function products()
{
  return $this->belongsToManu('Product')->withPivot('desired_column as desired_column');
  // this way you override aliasing the column to pivot_desired_column thus avoid it being attached to the pivot model
}

// Product model
protected $hidden = ['pivot'];

// execution

$category->products->toArray(); // the same for toJson()
// array(
//   0 => array(
//     'id' => '1',
//     ....
//     'created_at' => '2014-04-10 19:54:30',
//     'updated_at' => '2014-04-10 19:54:30',
//     'desired_column' => 'some value from pivot'
//     )
//   ),
//   ....
// )
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Clox clox Joined 13 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.