【发布时间】:2017-02-28 03:17:11
【问题描述】:
当 用户 尝试在其 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;
}
}
【问题讨论】:
-
$conference->sponsors()->save()看起来会导致错误。conference模型中如何定义这种关系? -
我也觉得!它曾经工作得很好。
-
我在下面添加了会议模型!
-
其实我们需要这个Event模型...
标签: php arrays laravel static dropdown