Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

share your models on http://laravel.io/bin...

Probably your reletions definitons can be wrong..

Last updated 2 years ago.
0

laravel.io/bin/aN1my

used tree/branch/leaf as easiest example to explain

Last updated 2 years ago.
0

(can't edit)

the place where i'm trying to get post (the leaf) is:

@foreach ($forum->category as $category)
....
{{ count($category->topic->post) }}
.... 
@endforeach

But like i noted above, it gives the error Undefined property on post. In what way can i access post?

Last updated 2 years ago.
0

Can you share migration file as you did ?

Last updated 2 years ago.
0

sure: http://laravel.io/bin/KkdDK

Thanks for helping by the way ^^

Last updated 2 years ago.
0

While searching i came across "hasManyThrough", this sort of skips a part or something? $category->post (instead of $category->topic->post)

Though cant seem to get this to work, you know this?

Last updated 2 years ago.
0

You can try foreing key to your migration like this http://laravel.io/bin/Yy2qV

In past I don't need "hasManyThrough" relationship our project. I have no experinces..

İf your problem is solved by adding foreing key, You can test relations by unit test..

An Example by using Unit Test from my projects


public function testRelationToProduct() {

           $model = new ProCategory;

           $this->assertInstanceOf('Product', $model->Products()->getRelated());
}


    public function testRelationToProCategory() {
        
        $model = new Product;
         
        $this->assertInstanceOf('ProCategory', $model->ProCategory()->getRelated());
        
        
    }
Last updated 2 years ago.
0

I also think that you maybe doesn't know UnitTest.

you can test the relations to models by adding this route to routes.php file


 Route::get('test-relations', function () {
    
   
        $category = new Category();
        
        return var_dump($category->getRelated() instanceof Topic);
               
        
    });

And than You can check get curl http://localhost/test-relations by via your web browser . Response is true ise reletionships is correct or not..

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

lorienhd lorienhd Joined 20 Mar 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.

© 2024 Laravel.io - All rights reserved.