2014-01-06 127 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']);