SQLSTATE[23000]:违反完整性约束:1048 列 'sponsorship_type' 不能为空
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sponsorship_type' cannot be null
当用户尝试add/store一个赞助商以及赞助类型 在他的 CMS 中,我收到此错误 SQLSTATE[23000]。我想知道这个错误的原因可能是什么。该行之前没有设置为空,我不应该是因为赞助商必须具有某种赞助类型。
这是我的观点:
<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>
控制器函数:
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();
}
型号:
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');
}
}
事件模型:
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;
}
}
看起来很简单。
Input::get('sponsorship_type')
此 returns null,您的数据库不喜欢。如果没有名为 sponsorship_type
.
的字段,这应该 return null 的唯一方法
所以看看你的 HTML,你有 <select name="sponsorship_types" class="form-control">
我认为您需要将此元素的 name
更改为 sponsorship_type
。
当用户尝试add/store一个赞助商以及赞助类型 在他的 CMS 中,我收到此错误 SQLSTATE[23000]。我想知道这个错误的原因可能是什么。该行之前没有设置为空,我不应该是因为赞助商必须具有某种赞助类型。
这是我的观点:
<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>
控制器函数:
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();
}
型号:
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');
}
}
事件模型:
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;
}
}
看起来很简单。
Input::get('sponsorship_type')
此 returns null,您的数据库不喜欢。如果没有名为 sponsorship_type
.
所以看看你的 HTML,你有 <select name="sponsorship_types" class="form-control">
我认为您需要将此元素的 name
更改为 sponsorship_type
。