首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >拉维雄辩地储存了许多到许多关系

拉维雄辩地储存了许多到许多关系
EN

Stack Overflow用户
提问于 2014-09-12 17:36:10
回答 1查看 749关注 0票数 0

我有三张桌子

代码语言:javascript
运行
复制
-- 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();

        });

代码语言:javascript
运行
复制
-- 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

代码语言:javascript
运行
复制
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

循环和保存

代码语言:javascript
运行
复制
1-3
1-4 

in campaign_networks_relationhsips

比我尝试下面的

代码语言:javascript
运行
复制
<?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')));
    }


}

而不是我有运动的存储库

代码语言:javascript
运行
复制
<?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');
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-12 18:54:03

A必须为您阅读:http://laravel.com/docs/eloquent#relationships

根据您所写的,我假设您希望保存一个模型及其枢轴关系,如下所示:

代码语言:javascript
运行
复制
// 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

就这样。要使上述工作正常进行,您需要设置以下关系:

代码语言:javascript
运行
复制
// Campaign model
public function networks()
{
  return $this->belongsToMany('Network', 'campaign_networks_relationhsips');
}

// Network model
public function campaings()
{
  return $this->belongsToMany('Campaign', 'campaign_networks_relationhsips');
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25813808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档