2014-09-12 62 views
0

我有3個表Laravels雄辯許多存儲一對多的關係

-- networks 
    Schema::create('networks', function(Blueprint $table) 
     { 
      $table->engine = 'InnoDB'; 

      $table->increments('id'); 
      $table->string('network'); 
      $table->string('description',255); 
      $table->string('attributes',255); 
      $table->timestamps(); 

     }); 

-- campaigns 

Schema::create('campaigns', function($table) { 
       $table->engine = 'InnoDB'; 
       $table->increments('id'); 
       $table->string('campaign_title'); 
       $table->integer('owner')->unsigned(); 
       $table->string('details'); 
       $table->timestamps(); 
      }); 

      Schema::table('campaigns', function($table) { 
       $table->foreign('owner')->references('id')->on('users')->onDelete('cascade'); 
      }); 

和 - campaign_networks_relationhsips

Schema::table('campaign_networks_relationhsips', function($table) 
    { 
     $table->foreign('campaign_id')->references('id')->on('campaigns')->onDelete('cascade'); 
     $table->foreign('network_id')->references('id')->on('networks')->onDelete('cascade'); 
    }); 

在新建立廣告活動,形式我將可用網絡顯示爲複選框。

用戶給出了該活動的標題,並將所需網絡廣告並保存。

問題:

具有雄辯這將直接拉動這個關係到綁定表(campaign_networks_relationhsips)的方法或我必須做2個查詢,如節約活動和獲取活動我的ID後在網絡上使用循環將這種關係保存在綁定表中。

例如

創建活動:給我回ID:1個 選用網絡3,4-

比環和保存

1-3 
1-4 

in campaign_networks_relationhsips 

比我嘗試以下

<?php namespace Td\Reports\Controllers\Backend; 

use Td\Reports\Campaigns\CampaignsInterface; 
use Input, 
    Redirect, 
    View, 
    App, 
    Str; 
use Illuminate\Support\MessageBag; 
class CampaignsController extends ObjectBaseAdminController { 

    /** 
    * The place to find the views/URL keys for this controller 
    * @var string 
    */ 
    protected $view_key = 'admin.campaigns'; 

    protected $networks; 

    /** 
    * Construct 
    */ 
    public function __construct(CampaignsInterface $campaigns) { 
     $this->model = $campaigns; 
     $networks = App::make('Td\Reports\Networks\NetworksInterface'); 
     $this->networks = $networks->getAll(); 
     parent::__construct(); 
    } 

    public function postNew() { 


     $record = $this->model->getNew(Input::all()); 
     //$record->campaign_title= Input::get('campaign_title'); 

     $valid = $this->validateWithInput === true ? $record->isValid(Input::all()) : $record->isValid(); 

     if (!$valid) 
      return Redirect::to('admin/' . $this->new_url)->with('errors', $record->getErrors())->withInput(); 

     // Run the hydration method that populates anything else that is required/runs any other 
     // model interactions and save it. 
     $record->save(); 

     $record->networks()->sync([3,4]); 

     return Redirect::to($this->object_url)->with('success', new MessageBag(array('Item Created'))); 
    } 


} 

比我擁有廣告系列的存儲庫

<?php 

namespace Td\Reports\Campaigns; 

use Td\Reports\Core\EloquentBaseRepository; 
use Td\Reports\Abstracts\Traits\NetworkableRepository; 
use Datatables,Sentry; 

class CampaignsRepository extends EloquentBaseRepository implements CampaignsInterface { 


    /** 
    * Construct 
    * @param Campaigns $campaigns 
    */ 
    public function __construct(Campaigns $campaigns) { 
     $this->model = $campaigns; 


    public function getAll() { 

     if (Sentry::getUser()->hasAnyAccess(['system'])) { 
      return $this->model->get(); 
     } else { 
      return $this->model->where('owner', Sentry::getUser()->id)->get(); 
     } 
    } 

    public function networks() 
    { 
     return $this->belongsToMany('Network', 'campaign_networks_relationhsips'); 
    } 

} 
+0

修改你的問題,你說什麼都沒有 – 2014-09-12 17:37:16

+0

我重新因式分解我的問題我給我的最好的 – fefe 2014-09-12 17:55:42

回答

1

一個必須供您閱讀:http://laravel.com/docs/eloquent#relationships

從你寫什麼,我想你要保存的模型和它的支點關係,所以這裏有雲:

// basic example flow in a controller, repo or wherever you like 
$campaign = new Campaign; 
$campaign->name = Input::get('name'); 
// assign more campaign attributes 
$campaign->save(); 

$campaign->networks()->sync([3,4]); // 3,4 are existing network rows ids 

這就是全部。對於上述工作,你需要設置這些關係:

// Campaign model 
public function networks() 
{ 
    return $this->belongsToMany('Network', 'campaign_networks_relationhsips'); 
} 

// Network model 
public function campaings() 
{ 
    return $this->belongsToMany('Campaign', 'campaign_networks_relationhsips'); 
} 
+0

是是感謝ü – fefe 2014-09-12 18:59:46

+0

我嘗試使用內您提供我的存儲庫,但我沒有去工作我打電話給未定義的方法Illuminate \ Database \ Query \ Builder :: sync()。 () { return $ this-> belongsToMany('Network','campaign_networks_relationhsips'); }我的活動存儲庫所以另一個到我的網絡存儲庫比從控制器我嘗試保存數據,但錯誤以上 – fefe 2014-09-16 13:51:22

+0

我不能幫你沒有代碼。該錯誤表示您正在嘗試對'Builder'對象調用'sync',而它是'BelongsToMany'方法。 – 2014-09-16 14:56:44