2014-09-12 61 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