laravel 5 belongsToMany作曲家更新后出错

时间:2015-09-21 14:04:30

标签: php laravel-5.1

我在作曲家更新后收到错误

  

Connection.php第636行中的QueryException:   SQLSTATE [42S22]:未找到列:1054未知列' war_id'在' where子句'

我有多对多关系的3个表

  1. 战争表
  2.   

    id - int pk

    1. 位置表
    2.   

      id - int pk

      1. war_locations表
      2.   

        id - int pk
          war_id int
          location_id int

        war_locations表是数据透视表。

        我在模型

        中声明了关系

        War.php

        public function locations()
        {
            return $this->belongsToMany('App\Models\Location', 'war_locations', 'war_id', 'location_id');
        }
        

        但是当我试图获得战争的位置时它会失败。

        $wars = War::with('locations')->where('status',1)->get();
        

        它在作曲家更新之前工作

        我不知道为什么它试图在数据透视表中找到war_id。 完全例外:

        `PDOException in Connection.php line 310:
        SQLSTATE[42S22]: Column not found: 1054 Unknown column 'war_id' in 'where clause'
        in Connection.php line 310
        at PDO->prepare('select * from `wc_locations` where `war_id` = ?') in Connection.php line 310
        at Connection->Illuminate\Database\{closure}(object(MySqlConnection), 'select * from `wc_locations` where `war_id` = ?', array('2')) in Connection.php line 629
        at Connection->runQueryCallback('select * from `wc_locations` where `war_id` = ?', array('2'), object(Closure)) in Connection.php line 596
        at Connection->run('select * from `wc_locations` where `war_id` = ?', array('2'), object(Closure)) in Connection.php line 315
        at Connection->select('select * from `wc_locations` where `war_id` = ?', array('2'), true) in Builder.php line 1389
        at Builder->runSelect() in Builder.php line 1379
        at Builder->getFresh(array('*')) in Builder.php line 1364
        at Builder->get(array('*')) in Builder.php line 409
        at Builder->getModels() in BelongsToMany.php line 176
        at BelongsToMany->get() in BelongsToMany.php line 99
        at BelongsToMany->getResults() in Model.php line 2699
        at Model->getRelationshipFromMethod('locations') in Model.php line 2665
        at Model->getRelationValue('locations') in Model.php line 2607
        at Model->getAttribute('locations') in Model.php line 3325
        at Model->__get('locations') in War.php line 102
        at War->getLocationListAttribute(null) in Model.php line 2722
        at Model->mutateAttribute('location_list', null) in Model.php line 2734
        at Model->mutateAttributeForArray('location_list', null) in Model.php line 2494
        at Model->attributesToArray() in Model.php line 2434
        at Model->toArray() in Collection.php line 902
        at Collection->Illuminate\Support\{closure}(object(War))
        at array_map(object(Closure), array(object(War), object(War), object(War), object(War), object(War), object(War))) in Collection.php line 904
        at Collection->toArray() in Collection.php line 914
        at Collection->jsonSerialize()
        at json_encode(object(Collection)) in 6823600b52dddc7dc45dcf9b454ab6d0 line 559
        at include('/home/aditya/public_html/warcircle/storage/framework/views/6823600b52dddc7dc45dcf9b454ab6d0') in PhpEngine.php line 42
        at PhpEngine->evaluatePath('/home/aditya/public_html/warcircle/storage/framework/views/6823600b52dddc7dc45dcf9b454ab6d0', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'wars' => object(Collection), 'user' => null, 'trendingWars' => array(object(War), object(War)), 'topWars' => object(Collection))) in CompilerEngine.php line 58
        at CompilerEngine->get('/home/aditya/public_html/warcircle/resources/views/site/home/index.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'wars' => object(Collection), 'user' => null, 'trendingWars' => array(object(War), object(War)), 'topWars' => object(Collection))) in View.php line 138
        at View->getContents() in View.php line 107
        at View->renderContents() in View.php line 81
        at View->render() in Response.php line 51
        at Response->setContent(object(View)) in Response.php line 202
        at Response->__construct(object(View)) in Router.php line 1225
        at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 113
        at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
        at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
        at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
        at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
        at ControllerDispatcher->callWithinStack(object(HomeController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 69
        at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\Site\HomeController', 'index') in Route.php line 201
        at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
        at Route->run(object(Request)) in Router.php line 704
        at Router->Illuminate\Routing\{closure}(object(Request))
        at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
        at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
        at Pipeline->then(object(Closure)) in Router.php line 706
        at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
        at Router->dispatchToRoute(object(Request)) in Router.php line 631
        at Router->dispatch(object(Request)) in Kernel.php line 236
        at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
        at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
        at VerifyCsrfToken->handle(object(Request), object(Closure))
        at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
        at ShareErrorsFromSession->handle(object(Request), object(Closure))
        at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
        at StartSession->handle(object(Request), object(Closure))
        at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
        at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
        at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
        at EncryptCookies->handle(object(Request), object(Closure))
        at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
        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 124
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
        at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
        at Pipeline->then(object(Closure)) in Kernel.php line 122
        at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
        at Kernel->handle(object(Request)) in index.php line 54`
        

1 个答案:

答案 0 :(得分:0)

看来你们关系中的钥匙顺序错误。尝试交换war_id和location_id,如下所示:

public function locations()
{
    return $this->belongsToMany('App\Models\Location', 'war_locations', 'location_id', 'war_id');
}