Reputation: 4305
Hi all i am trying to create a campaign. In the process of campaign i will assign certain products to that campaign. So i have to insert details after post campaign creation into two tables one is campaigns
and another one is campaignsproducts
. So i created two models for each of them on the same names.
I am trying to insert records into both tables on post action using save method. I am able to insert into campaigns
but when it comes to campaignsproducts
it says campaigns_products
table not exists.
In my db my table name was CampaignsProducts
. Please help where i am going wrong. Please find my migration, model and post action code below.
<?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');
}
}
Model CampaignProducts.php
<?php
class CampaignProducts extends Eloquent {
public function camapigns(){
return $this->hasMany('Campaign');
}
public function products(){
return $this->hasMany('Product');
}
}
Post action in controller
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');
}
}
Upvotes: 0
Views: 2353
Reputation: 60048
Add this to your CampaignProducts model:
class CampaignProducts extends Eloquent {
protected $table = 'campaignsproducts';
...
Or the other option is to change your table name when you create it instead:
Schema::create('campaign_products', function(Blueprint $table)
Upvotes: 4