采用错误的表名的Laravel保存方法

时间:2014-06-22 06:26:28

标签: php laravel-4

大家好我想创建一个广告系列。在广告系列的过程中,我会将某些产品分配给该广告系列。因此,我必须在广告系列制作后将详细信息插入两个表格中,一个是campaigns,另一个是campaignsproducts。所以我为每个人创建了两个相同名称的模型。

我正在尝试使用save方法在post action上将记录插入到两个表中。我可以插入campaigns但是当涉及campaignsproducts时,它表示campaigns_products表不存在。

在我的数据库中,我的表名是CampaignsProducts。请帮助我出错的地方。请在下面找到我的迁移,模型和发布操作代码。

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCamapignproductsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('campaignproducts', function(Blueprint $table)
        {
            $table->increments('id');

            $table->integer('campaign_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->decimal('product_sell_cost', 10, 2);

            $table->timestamps();
        });

        Schema::table('campaignproducts', function($table) {
            $table->foreign('campaign_id')->references('id')->on('campaigns');
            $table->foreign('product_id')->references('id')->on('products');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('campaignproducts');
    }

}

模型CampaignProducts.php

<?php

class CampaignProducts extends Eloquent {

    public function camapigns(){
        return $this->hasMany('Campaign');
    }

    public function products(){
        return $this->hasMany('Product');
    }
}

在控制器中发布操作

public function postCampaign()
{
    //validation rules for create product 
    $rules = array(
        'campaign_name'         => 'required|min:2',
        'campaign_description'  => 'required|min:2',
        'campaign_startdate'    => 'required|date_format:"Y-m-d"',
        'campaign_enddate'      => 'required|date_format:"Y-m-d"',
        'campaign_urlname'      => 'required|between:4,20',
        'campaign_target'       => 'required|integer|min:1',
        'user_id'               => 'required|integer|min:1'
    );

    $validator = Validator::make(Input::all(), $rules);

    //procee the validation rules
    if($validator->fails()) {
        return Redirect::to('products/newcampaign')
            ->withErrors($validator)
            ->withInput();
    } else {
        echo "<pre>";
        print_r(Input::all());

        //store category data
        $campaign = new Campaign;
        $campaign->campaign_name            = Input::get('campaign_name');
        $campaign->campaign_description     = Input::get('campaign_description');
        $campaign->campaign_startdate       = Input::get('campaign_startdate');
        $campaign->campaign_enddate         = Input::get('campaign_enddate');
        $campaign->campaign_urlname         = Input::get('campaign_urlname');
        $campaign->campaign_target      = Input::get('campaign_target');
        $campaign->user_id              = Input::get('user_id');

        $campaign_id = $campaign->save();
        $campaign_products = Input::get('productid');

        $campaignproducts = new CampaignProducts;
        foreach($campaign_products as $key => $id)
        {
            $campaignproducts->product_id       = $key;
            $$campaignproducts->product_sell_cost = $id;
            $campaignproducts->campaign_id =    $campaign_id;
            $campaignproducts->save();  
        }       

        //redirect
        Session::flash('message', 'Successfully created campaign!');
        return Redirect::to('campaigns');
    }
}

1 个答案:

答案 0 :(得分:4)

将此添加到您的CampaignProducts模型中:

class CampaignProducts extends Eloquent {

    protected $table = 'campaignsproducts';

    ...

或者另一种选择是在创建表名时更改表名:

Schema::create('campaign_products', function(Blueprint $table)