dongxin2734 2016-06-16 11:19
浏览 52

如何在laravel框架中创建三元关系

I need to create triadic relation in laravel framework.

Consider three models: Card, Notes, User.

Card is related to notes and user, while they are related to each other. Now in models, when the relation ship would be drafted, Card->belongTo -> Notes Notes-> hasMany -> Card

The problem is when relationship between card and user is created, keeping to above rules,the connection is not made.

user -> hasMany ->card; Card ->belongTo -> user.

stacktrace:

1/1 MethodNotAllowedHttpException in RouteCollection.php line 218:

    in RouteCollection.php line 218
    at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 205
    at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 158
    at RouteCollection->match(object(Request)) in Router.php line 821
    at Router->findRoute(object(Request)) in Router.php line 691
    at Router->dispatchToRoute(object(Request)) in Router.php line 675
    at Router->dispatch(object(Request)) in Kernel.php line 246
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
    at CheckForMaintenanceMode->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102
    at Pipeline->then(object(Closure)) in Kernel.php line 132
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
    at Kernel->handle(object(Request)) in index.php line 53
    at require_once('/home/sayali/people_finder/public/index.php') in server.php line 21

my user.php - has a func Cards

public function cards()
{
    return $this->hasMany(Note::class);
}

in cards.php

public function user()
{
    return $this->belongsTo(User::class);
}
  • 写回答

1条回答 默认 最新

  • dongwo5589 2016-06-16 11:42
    关注

    Based on your stacktrace, you're not submitting information in a POST request or you route isn't responding on POST requests. Check this thread on the Laravel forums for some more explanation.

    Basically make sure your form submit method and route response method are the same thing.

    I'm still not a huge proponent of your triadic relation though. I'd give every card a user OR give a user all his cards and then work from there. 2-way relations are bound to get messed up in my experience.

    评论

报告相同问题?