How to create self referential relationship in laravel?

Laravel 4

Laravel 4 Problem Overview


I am new to Laravel. I Just want to create a self referential model. For example, I want to create a product category in which the field parent_id as same as product category id. How is this possible?

Model Shown below

class Product_category extends Eloquent 
{
	protected $guarded = array();

	public static $rules = array(
		'name' => 'required',
		'parent_id' => 'required'
	);

	 function product_category()
	{
		return $this->belongsto('Product_category','parent_id');
	}
}

It results Maximum function nesting level of '100' reached, aborting! Error

Laravel 4 Solutions


Solution 1 - Laravel 4

You can add a relation to the model and set the custom key for the relation field.

Update:

Try this construction

class Post extends Eloquent {

    public function parent()
    {
        return $this->belongsTo('Post', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('Post', 'parent_id');
    }
}

Old answer:

class Post extends Eloquent {

    function posts(){
        return $this->hasMany('Post', 'parent_id');
    }
}

Solution 2 - Laravel 4

Your model is not at fault for producing the "maximum function nesting level of '100' reached" error. It's XDebug's configuration; increase your xdebug.max_nesting_level.

The following is from a 2015 post by @sitesense on laracasts.com:

>This is not a bug in Laravel, Symfony or anything else. It only occurs when XDebug is installed. > >It happens simply because 100 or more functions are called recursively. This is not a high figure as such and later versions of XDebug (>= 2.3.0) have raised this limit to 256. See here: > >http://bugs.xdebug.org/bug_view_page.php?bug_id=00001100 > >EDIT: In fact the latest Homestead provisioning script already sets the limit to 250. See line 122 here: > >https://github.com/laravel/settler/blob/master/scripts/provision.sh#L122

So the addition of xdebug.max_nesting_level = 250 to php.ini should do it.

Solution 3 - Laravel 4

I've added a little more to the code based on your comments trying to access the parent!

class Person extends \Eloquent {
    protected $fillable = [];
    var $mom, $kids;
    
    function __construct() { 
        if($this->dependency_id<>0) {
            $this->mother->with('mother');	
        }
    }
    
    public function children() {
        $children = $this->hasMany('Person','dependency_id');
        foreach($children as $child) {
            $child->mom = $this;
        }
        return 	$children;
    }

    public function mother() {
        $mother = $this->belongsTo('Person','dependency_id');
        if(isset($mother->kids)) {
            $mother->kids->merge($mother);
        }
        return $mother;
    }
}

Then you can access the parent from the child with eager loading, see more here: http://neonos.net/laravel-eloquent-model-parentchild-relationship-with-itself/

Solution 4 - Laravel 4

you can refer self, using $this

class Post extends Eloquent {

    function posts(){
        return $this->hasMany($this, 'parent_id');
    }
}

Solution 5 - Laravel 4

Take a look at my answer here. The key is this code below in Model.php

public function children()
    {
        return $this->hasMany(Structure::class, 'parent_id')->with('children');
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPraveenView Question on Stackoverflow
Solution 1 - Laravel 4JackPointView Answer on Stackoverflow
Solution 2 - Laravel 4Sam WilsonView Answer on Stackoverflow
Solution 3 - Laravel 4NeoView Answer on Stackoverflow
Solution 4 - Laravel 4Sreekanth RamachandranView Answer on Stackoverflow
Solution 5 - Laravel 4Mohammad Iqbal RamadhanView Answer on Stackoverflow