Laravel Removing Pivot data in many to many relationship

Rob

Not sure if I set this up correctly. In Laravel I'm creating two models with a many-to-may relationship

The models are Item and Tags. Each one contains a belongsTo to the other.

When I run a query like so:

Item::with('tags')->get();

It returns the collection of items, with each item containing a tags collection. However the each tag in the collection also contains pivot data which I don't need. Here it is in json format:

[{
    "id":"49",
    "slug":"test",
    "order":"0","tags":[
        {"id":"3","name":"Blah","pivot":{"item_id":"49","tag_id":"3"}},
        {"id":"13","name":"Moo","pivot":{"item_id":"49","tag_id":"13"}}
    ]
}]

Is there anyway to prevent this data from getting at

lukasgeiter

You have asked and you shall receive your answer. But first a few words to sum up the comment section. I personally don't know why you would want / need to do this. I understand if you want to hide it from the output but not selecting it from the DB really has no real benefit. Sure, less data will be transferred and the DB server has a tiny tiny bit less work to do, but you won't notice that in any way.

However it is possible. It's not very pretty though, since you have to override the belongsToMany class.

First, the new relation class:

class BelongsToManyPivotless extends BelongsToMany {
    /**
     * Hydrate the pivot table relationship on the models.
     *
     * @param  array  $models
     * @return void
     */
    protected function hydratePivotRelation(array $models)
    {
        // do nothing
    }

    /**
     * Get the pivot columns for the relation.
     *
     * @return array
     */
    protected function getAliasedPivotColumns()
    {
        return array();
    }
}

As you can see this class is overriding two methods. hydratePivotRelation would normally create the pivot model and fill it with data. getAliasedPivotColumns would return an array of all columns to select from the pivot table.

Now we need to get this integrated into our model. I suggest you use a BaseModel class for this but it also works in the model directly.

class BaseModel extends Eloquent {

    public function belongsToManyPivotless($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null){
        if (is_null($relation))
        {
            $relation = $this->getBelongsToManyCaller();
        }

        $foreignKey = $foreignKey ?: $this->getForeignKey();

        $instance = new $related;

        $otherKey = $otherKey ?: $instance->getForeignKey();

        if (is_null($table))
        {
            $table = $this->joiningTable($related);
        }

        $query = $instance->newQuery();

        return new BelongsToManyPivotless($query, $this, $table, $foreignKey, $otherKey, $relation);
    }
}

I edited the comments out for brevity but otherwise the method is just like belongsToMany from Illuminate\Database\Eloquent\Model. Of course except the relation class that gets created. Here we use our own BelongsToManyPivotless.

And finally, this is how you use it:

class Item extends BaseModel {
    public function tags(){
        return $this->belongsToManyPivotless('Tag');
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Many to many relationship get specific data

From Dev

Laravel many to many relationship, add more objects into "pivot"?

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

From Dev

Laravel - Order by pivot value in Many to Many table relationship

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Unable to sync conditional pivot table many to many relationship laravel

From Dev

Laravel L5.5 No access to pivot table in "Many to many" relationship

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Laravel: Get pivot data for specific many to many relation

From Dev

Laravel 5 - Many to many relations - Getting the pivot data

From Dev

Laravel 6 many to many relationship

From Dev

Pagination with many to many relationship in laravel

From Dev

Laravel many to many relationship error

From Dev

Laravel many to many relationship with conditions

From Dev

Laravel ORM many to many relationship

From Dev

Laravel Get data One to Many Relationship with conditions

From Dev

Laravel eloquent one to many relationship in custom pivot tables

From Dev

Empty data returning from many to many relationship laravel 5

From Dev

Creating a Many-to-many relationship in Laravel with additional data

From Dev

Laravel: one to many relationship

From Dev

one to many relationship in laravel

From Dev

Laravel 4.2 Many-to-many relationship : Can't read from pivot table

From Dev

Laravel 4 - Many to Many - pivot table not updating

Related Related

  1. 1

    Laravel Many to many relationship get specific data

  2. 2

    Laravel many to many relationship, add more objects into "pivot"?

  3. 3

    Laravel Eloquent Many to Many relationship using a pivot table

  4. 4

    Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

  5. 5

    Laravel - Order by pivot value in Many to Many table relationship

  6. 6

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  7. 7

    Unable to sync conditional pivot table many to many relationship laravel

  8. 8

    Laravel L5.5 No access to pivot table in "Many to many" relationship

  9. 9

    Laravel - Many to Many relationship

  10. 10

    Many to many relationship in Laravel

  11. 11

    Many to Many relationship with Laravel

  12. 12

    How to query a pivot table data in MySQL (many to many relationship)

  13. 13

    Creating one to many relationship with pivot table of many to many relationship

  14. 14

    Creating one to many relationship with pivot table of many to many relationship

  15. 15

    Laravel: Get pivot data for specific many to many relation

  16. 16

    Laravel 5 - Many to many relations - Getting the pivot data

  17. 17

    Laravel 6 many to many relationship

  18. 18

    Pagination with many to many relationship in laravel

  19. 19

    Laravel many to many relationship error

  20. 20

    Laravel many to many relationship with conditions

  21. 21

    Laravel ORM many to many relationship

  22. 22

    Laravel Get data One to Many Relationship with conditions

  23. 23

    Laravel eloquent one to many relationship in custom pivot tables

  24. 24

    Empty data returning from many to many relationship laravel 5

  25. 25

    Creating a Many-to-many relationship in Laravel with additional data

  26. 26

    Laravel: one to many relationship

  27. 27

    one to many relationship in laravel

  28. 28

    Laravel 4.2 Many-to-many relationship : Can't read from pivot table

  29. 29

    Laravel 4 - Many to Many - pivot table not updating

HotTag

Archive