1

I found this function in Internet and it works.

In my App

public function childs(){
    return $this->hasMany('App\Account','p_id');
}

In my route:

Route::get('tests', function(){
return App\Account::with('childs')
->where('p_id',0)
->get();
});

So if you have in the dabase a raw with p_id = 0 and id = 1 it return a category and if you have a raw with p_id = 1 and id = 2 that mean that the child of the categorie with id = 1.

Another example:: If you have a raw with p_id = 6 and id = 16 that mean that the child of the category with id = 6

Please who can explain to mean why exactly it work like that ?

1 Answer 1

0

Laravel Relationship are not used to make relations between 1 model. You cannot make relation between Account and Account. An account does not has "hasMany" accounts.

You can have hasMany users for example.

public function childs(){
   return $this->hasMany('App\User','p_id');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, you can call parent/children relation on model. Here is explained too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.