2014-01-06 85 views
0

我无法将额外数据保存(创建新行)到数据透视表。laravel 4数据透视表保存不起作用

我不得不使用传统的数据库模式(这仍然是一个活的网站上工作)。我目前正在Laravel重做该网站。

<?php namespace Carepilot\Repositories\Organizations; 

use Carepilot\Repositories\EloquentRepositoryAbstract; 

/* 
* This class is the Eloquent Implementation of the Organization Repository 
*/ 

class OrganizationRepositoryEloquent extends EloquentRepositoryAbstract implements OrganizationRepositoryInterface { 


    /* 
    * Define Eloquent Organization Type Relation. 
    * 
    */ 
    public function orgtypes() 
    { 
     return $this->belongsToMany('Carepilot\Repositories\OrganizationTypes\OrganizationTypesRepositoryEloquent', 'organizations_orgtypes', 'organization_id', 'orgtype_id') 
      ->withPivot('objectstate_id'); 
    } 

} 


<?php namespace Carepilot\Repositories\OrganizationTypes; 

use Carepilot\Repositories\EloquentRepositoryAbstract; 

/* 
* This class is the Eloquent Implementation of the Organization Repository 
*/ 

class OrganizationTypesRepositoryEloquent extends EloquentRepositoryAbstract implements OrganizationTypesRepositoryInterface { 

    protected $table = 'orgtypes'; 

    /* 
    * Define Eloquent Organization Type Relation. 
    * 
    */ 
    public function organizations() 
    { 
     return $this->belongsToMany('Carepilot\Repositories\Organizations\OrganizationRepositoryEloquent', 'organizations_orgtypes', 'organization_id', 'orgtype_id') 
      ->withPivot('objectstate_id'); 
    } 

} 

在这里,在Orgaizations控制器我尝试保存一个新的组织,但在数据透视表得到一个错误。

<?php 

use \Carepilot\Repositories\Organizations\OrganizationRepositoryInterface; 
use \Carepilot\Repositories\Procedures\ProcedureRepositoryInterface; 
use \Carepilot\Helpers\StatesHelper; 

class OrganizationsController extends BaseController { 

    /** 
    * Organization Repository 
    * 
    * @var organization_repo 
    */ 
    protected $organization_repo; 
    protected $procedures; 
    protected $states; 

    public function __construct(OrganizationRepositoryInterface $organization_repo, 
           ProcedureRepositoryInterface $procedures, 
           StatesHelper $states) 
    { 
     $this->organization_repo = $organization_repo; 
     $this->procedures = $procedures; 
     $this->states = $states; 
    } 

    /** 
    * Display a listing of the resource. 
    * 
    * @return Response 
    */ 
    public function index() 
    { 
     return View::make('organizations.index'); 
    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return Response 
    */ 
    public function create() 
    { 
     $supportedStates = ['' => 'Choose'] + $this->states->supportedStates(); 
     $procedures = $this->procedures->getDropDown(); 
     return View::make('organizations.create', compact('supportedStates', 'procedures')); 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @return Response 
    */ 

    public function store() 
    { 
     $input = Input::all(); 
     // validation here 

     $new_organization = $this->organization_repo->create(array_only($input, ['organization_name'])); 
     $input['objectstate_id'] = 27; 
     $input['orgtype_id'] = 1; 
     $new_organization->orgtypes->pivot->create(array_only($input, ['objectstate_id', 'orgtype_id'])); 
     $new_organization->addresses()->create(array_only($input, ['street1', 'zip', 'city', 'state_code'])); 
     $input['objectstate_id'] = 4; 
     $new_organization->users()->create(array_only($input, ['first_name', 'last_name', 'email', 'password', 'objectstate_id'])); 

     return "completed"; 

    } 

这将返回“未定义的属性:照亮\数据库\雄辩\收藏:: $支点”,因为枢轴具有尚未创建我缺少什么

回答

0

我发现,‘附加’。?方法做我想要的东西像

$new_organization->orgtypes()->attach(1, ['objectstate_id' => '27']);