【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'sponsorship_type' cannot be nullSQLSTATE [23000]:违反完整性约束:1048 列 'sponsorship_type' 不能为空
【发布时间】: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-&gt;sponsors()-&gt;save() 看起来会导致错误。 conference 模型中如何定义这种关系?
  • 我也觉得!它曾经工作得很好。
  • 我在下面添加了会议模型!
  • 其实我们需要这个Event模型...

标签: php arrays laravel static dropdown


【解决方案1】:

看起来很简单。

Input::get('sponsorship_type')

这将返回您的数据库不喜欢的 null。这应该返回 null 的唯一方法是如果没有名为 sponsorship_type 的字段。

所以看看你的 HTML,你有 &lt;select name="sponsorship_types" class="form-control"&gt;

我认为您需要将此元素的name 更改为sponsorship_type

【讨论】:

  • 解决了这个问题!谢谢!很好的收获。
猜你喜欢
  • 2021-02-01
  • 2019-11-20
  • 2021-08-16
  • 2019-10-06
  • 2020-10-25
  • 2017-04-15
  • 2017-12-26
  • 2019-11-24
  • 2018-01-25
相关资源
最近更新 更多