我有三张桌子
-- 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),或者我必须进行两个查询,比如保存活动,在获得活动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');
}
}发布于 2014-09-12 18:54:03
A必须为您阅读: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');
}https://stackoverflow.com/questions/25813808
复制相似问题