我有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');
}
}
修改你的問題,你說什麼都沒有 – 2014-09-12 17:37:16
我重新因式分解我的問題我給我的最好的 – fefe 2014-09-12 17:55:42