I have three table, campaign, places and campaign_place, the primary keys in campaign and place are used inside campaign_place as foriegn keys plus the campaign_place table has it's own id column and the user_id column.
I have setup the proper many to many relationships using BelongsToMany in campaign and place models.
1- I define a number of places, and then I go and create a campaign. I have a multi select box which is populated by the places created by the logged in user.
2- What I am trying to achieve is that when I save the campaign the places selected in the multiselect are also saved in the associative table with user_id.
currently I am successfully saving all the data in campaigns, places and in the associative table of campaign_place as well.
The only thing I am confused about is saving the user_id in the associative table as well because at this time its not being saved.
Following is the code:
Create form for campaigns:
{!! Form::open(array('route' => 'campaigns.store')) !!}
{{Form::label('campaignName','Campaign Name:' )}}
{{Form::text('campaignName', null, array('class' => 'form-control', 'placeholder'=>'Name of your campaign')) }}
{{Form::label('campaignDescription','Campaign Description:' )}}
{{Form::textarea('campaignDescription', null, array('class' => 'form-control', 'rows' => '2', 'placeholder'=>'Provide a small description of your campaign...')) }}
<div class="col-lg-6">
{{Form::label('startDate','Start Date:' )}}
{{Form::date('startDate', null, array('class' => 'form-control')) }}
</div>
<div class="col-lg-6">
{{Form::label('endDate','End Date:' )}}
{{Form::date('endDate', null, array('class' => 'form-control')) }}
</div>
{{Form::label('title','Campaign Title:' )}}
{{Form::text('title', null, array('class' => 'form-control', 'id' => 'title','placeholder'=>'This will appear in your mobile')) }}
{{Form::label('places','Assign Places:' )}}
<select class="form-control select2-multi" name="places[]" multiple="multiple">
@foreach (auth()->user()->places as $place)
<option value='{{ $place->id }}'>{{ $place->name }}</option>
@endforeach
</select>
{{Form::label('notificationText','Notification Text:' )}}
{{Form::text('notificationText', null, array('class' => 'form-control')) }}
{{Form::label('body','Campaign Body:' )}}
{{Form::textarea('body', null, array('class' => 'form-control', 'rows' => '3')) }}
{{Form::hidden('user_id', Auth::user()->id)}}
{{Form::submit('Save Campaign', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;'))}}
{!! Form::close() !!}
Code for the models:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Campaign extends Model
{
protected $fillable=['user_id','campaignName','campaignDescription','startDate','endDate','title','notificationText','body'];
public function places()
{
return $this->belongsToMany('App\Place');
}
}
Place model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Place extends Model
{
protected $fillable=['name','description','ownedssid','ownedmacaddress', 'ownedrssival', 'lat', 'lng','gfenceradius','country','city','citycode'];
public function campaigns()
{
return $this->belongsToMany('App\Campaign');
}
}
Code for store function in campaign controller:
public function store(Request $request)
{
// dd($request);
$this->validate($request, array(
'campaignName' =>'required|max:255',
'startDate' =>'required',
'endDate' =>'required',
'title' =>'required',
'notificationText' =>'required',
'body' =>'required'
));
$user = Auth::user();
$campaign = new Campaign;
$campaign->campaignName = $request->campaignName;
$campaign->campaignDescription = $request->campaignDescription;
$campaign->startDate = $request->startDate;
$campaign->endDate = $request->endDate;
$campaign->title = $request->title;
$campaign->notificationText = $request->notificationText;
$campaign->body = $request->body;
$campaign->user_id = $request->user_id;
$campaign->save();
$campaign->places()->sync($request->places, false);
}
Can you please help!
thanks in advance