ajax_27 ajax_27 - 1 month ago 5
PHP Question

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sponsorship_type' cannot be null

When the User tries to add/store an sponsor along with sponsorship type in his CMS, I get this error SQLSTATE[23000]. Im wondering what could be the reason for this error. The row wasnt previously set to null, and I it shouldnt be because the sponsor must have a certain sponsorship type.

This is my view:

<div class="panel-body">

<div class="panel-body list-group list-group-contacts">
@foreach($event->sponsors as $sponsor)
{{ Form::open(array( 'route' => array( 'remove.sponsor.from.conference' , $event->id ) , 'method' => 'DELETE' )) }}
{{ Form::hidden('sponsor' , $sponsor->id) }}

<a href="#" class="list-group-item">
<span class="contacts-title">{{ $sponsor->name }}</span> <br>
<span class="contacts-title">{{ \Events\Models\Sponsor::$_SPONSORSHIP_TYPES[$sponsor->pivot->sponsorship_type][0] }}</span>
<div class="list-group-controls">
{{ Form::submit('X' , array( 'class' => 'btn btn-primary btn-rounded' )) }}
</div>
</a>
<br/>

{{ Form::close() }}

@endforeach
</div>

<h2>Add Sponsor</h2>

{{ Form::open(array( 'route' => array('add.sponsor.to.conference' , $event->id) , 'method' => 'POST')) }}
<select name="sponsor" class="form-control">
<option value="">Choose sponsor</option>

@foreach($sponsors as $sponsor)

<option value="{{$sponsor->id}}">{{ $sponsor->name }}</option>

@endforeach
</select>
<br>
<select name="sponsorship_types" class="form-control">
<option value="">Choose sponsorship type</option>
@foreach(\Events\Models\Sponsor::$_SPONSORSHIP_TYPES as $key => $value)

<option value="{{ $key }}">{{ $value[0] }}</option>

@endforeach
</select>
<br>


Controller function:

public function addSponsorToConference($event_id)
{
if (Input::get('sponsor') != "" && is_numeric(Input::get('sponsor')) )
{
$sponsor = Sponsor::find(Input::get('sponsor'));
$conference = Event::find($event_id);
$conference->sponsors()->save($sponsor, array('sponsorship_type' => Input::get('sponsorship_type')));
}
return Redirect::back();
}


public function removeSponsorFromConference($event_id)
{
$conference = Event::find($event_id);
$conference->sponsors()->detach(Input::get('sponsor'));
return Redirect::back();
}


Model:

class Sponsor extends BaseModel {


public static $_SPONSORSHIP_TYPES = array(
1 => ['Platinum sponsors', 400, 150],
2 => ['Gold sponsors', 300, 100],
3 => ['Silver sponsors', 200, 80],
4 => ['Bronze sponsors', 150, 70],
5 => ['Media partner', 60, 60],
);

protected $table = 'sponsors';

protected $fillable = ['name', 'logo', 'link', 'description'];

public function events()
{
return $this->belongsToMany('\Events\Models\Conference');
}

}


Event Model:

class Event extends BaseModel {

protected $table = 'events';

protected $fillable = ['title', 'slug', 'description', 'ticket_limit', 'location', 'gmap_location'];

public function sponsors()
{
return $this->belongsToMany('\Events\Models\Sponsor')->withPivot("sponsorship_type");
}

public function getSponsorsForShow()
{
$return = array();
foreach(Sponsor::$_SPONSORSHIP_TYPES as $key => $value){
$sponsors = $this->sponsors()->where("sponsorship_type", "=", $key)->get();
if(count($sponsors)) $return[] = array("height" => $value[2], "width" => $value[1], "sponsors" => $sponsors, "sponsorship_title"=>$value[0]);
}

return $return;
}

}

Answer

Looks like a simple fix.

Input::get('sponsorship_type')

This returns null which your database doesn't like. The only way this should return null is if there was no field named sponsorship_type.

So looking at your HTML, you have <select name="sponsorship_types" class="form-control">

I think you need to change the name of this element to sponsorship_type.