Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent
Last updated 9 months 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 9 months ago.
0

Sign in to participate in this thread!

LaraJobs

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.

© 2023 Laravel.io - All rights reserved.