Laravel Many to Many Self Referencing Table Only Works One Way

Laravel: Add a join to self referencing Many-to-Many relationship?

After going round and round with custom pivot models, all sorts of convoluted relations, etc., I believe I've found a simple solution—just add a select statement to your relation, targeting the affiliation_types table, so that you have access to the types column:

return $this->belongsToMany('TmpProd', 'tmp_prod2prod', 'p_id_to', 'p_id_from')
->withPivot('affiliation_type_id')
->join('affiliation_types', 'tmp_prod2prod.affiliation_type_id', '=', 'affiliation_types.id')
->addSelect('affiliation_types.type as affiliation_type');

(The as affiliation_type part is optional, but will make your query clearer semantically.)

Then, in your foreach loop, you can call:

var_dump($cross->affiliation_type);

It would also be a good idea to eager-load your affiliatedToProducts relations, to reduce the number of queries from N+1 down to 2. So, to find a particular product by ID:

$model = TmpProd::with('affiliatedToProducts')->find(1);

Or, if you need to retrieve all products:

$model = TmpProd::with('affiliatedToProducts')->get();

...then loop over the results with foreach ($model as $m).


Update: This worked for Laraval v4.2.8, but as of v4.2.11, it seems (counterintuitively) that ->addSelect replaces, rather than adds, the query's original select statement, and is functionally identical to just using ->select. So for versions newer than 4.2.8, it is necessary to add the original table back into the query, so the belongsToMany relation should end with:

->select('affiliation_types.type', 'tmp_prods.*');

creating self referencing entities in laravel 5.5

Finally i found solution, self referencing entity can be done with itself with Many To Many relation.

Model relation look like this :

class BaseDrink extends Model {

public function drinkbase(){

return $this
->belongsToMany('App\BaseDrink', 'basedrink_basedrink','basedrink_id', 'parent_basedrink_id')
->withPivot('created_by')
->withPivot('last_modified_by')
->withPivot('id')
->withTimestamps();
}
}

laravel belongsToMany self referencing attaching Relation

I can answer myself.. the relation has to be:

# Building Require one or more another Building(s)
public function requires() {
return $this->belongsToMany('Building', 'building_require', 'building_id', 'require_id')->withPivot(array(
'level',
'updated_at',
'created_at'
));
}

perhaps somebody will help it.

Relationship: belongs to only for filtered result

You can add in your user model this code

public function activeSubscribes()
{
return $this->whereHas('subscribes', function ($q){
$q->where('accept', 1); //your condition
});
}

public function subscribes()
{ //Also maybe you need hasOne relation
return $this->hasMany(Subscribes::class);
}

Then you can use it like this

User::activeSubscribes()->get()

Laravel belongsToMany Relationship issue

What you're asking is Table self reference for many to many relation. In your case the issue is that you are saving in the pivot table an from id to an other. So it can only retrieve in this way. With only one relation you have a problem of key order to be able to fetch them in both ways.

Have a look at this (you ends up with 2 methods to trigger the relation both way) : Laravel Many to many self referencing table only works one way

Better solution here : https://github.com/laravel/framework/issues/441

Laravel 4 how to set up many-to-many relationship with the same table

What about

resources
id - integer
name - string
description - string

resource_links
id - integer
resource_id - integer
linked_resource_id - integer

This makes it a bit clearer to anyone looking at the pivot what it is doing. Then you can retrieve the linked resources for a given resource ($resource_id below) with something like

$linked_resources = DB::table('resources')
->join('resource_links', 'resources.id', '=', 'resource_links.linked_resource_id')
->where('resource_links.resource_id', '=', $resource_id)
->get();

Edit:

Or you could add a many to many relationship on the model back to itself

public function linkedResources()
{
return $this->belongsToMany('Resource', 'resource_links', 'resource_id', 'linked_resource_id');
}

And then return the linked resources with

Resource::find(1)->linked_resources; 

Edit:

If you wanted to limit the linked resources by type something like the following is one approach

$linked_resources = Resource::with(array('linkedResources' => function($query) {
$query->where('type', '=', 'book');
}))->where('id','=',1)->get();


Related Topics



Leave a reply



Submit