Laravel:JSON发布多对多关系错误:SQLSTATE[23000]:违反完整性约束


Laravel: JSON to post many to many relations Error: SQLSTATE[23000]: Integrity constraint violation

我正在尝试从JSON发送post数据,除尝试从我的Many to Many关系发送数据外,其他一切都正常。

(我用的是UUID

这是我的错误:

SQLSTATE[23000]:完整性约束冲突:1048列"idResource"不能为null(SQL:插入到CTL_Resource_has_TagsidResourceidTag)值(,0119a3b2-4da-11e6-9079-6971fda2300c))

这是我正在尝试post的JSON,其中tagsquickTagsrelatedTo是我的Many to Many关系,我只是不知道如何发送它,或者我是否同意这个JSON,或者我试图存储数据的方式。

{
  "idResourceType": "0122b128-04da-11e6-9079-6971fda2300c",
  "idCreatorUser": "011d5c46-04da-11e6-9079-6971fda2300c",
  "idModifierUser": "011d5c46-04da-11e6-9079-6971fda2300c",
  "idCreationCountry": "011f14b4-04da-11e6-9079-6971fda2300c",
  "title": "234234324",
  "description": "asfjñwlejñlr howoowowowowoowowow",
  "thumbnail": null,
  "minimumAge": 1,
  "maximumAge": null,
  "fileName": null,
  "extension": null,
  "coachVisibility": "VIS",
  "studentVisibility": "VIS",
  "isHTML": null,
  "studentIndex": "",
  "coachIndex": "",
  "isURL": "on",
  "source": null,
  "path": null,
  "status": "ACT",
  "createTime": "2016-04-18 19:23:18",
  "updateTime": "0000-00-00 00:00:00",
  "isfolder": null,
  "parentResource": null,
  "productionKey": "073073097303798",
  "creatorUser": "Tamia Wilderman",
  "creationCountry": "Tanzania",
  "resourceType": "Infografía",
  "tags": [],
  "quickTags": [],
  "relatedTo": []
}

这是我在控制器中尝试存储数据的功能:

  public function store(Request $request) {

    $resource = new CTL_Resource($request->all());
    $resource->quickTags()->sync($request->quickTags);
    $resource->tags()->sync($request->tags);
    $resource->relatedTo()->sync($request->relatedTo);
  //or $resource->quickTags()->attach($request->quickTags);

    $resource->save();
    return response($resource->load('tags', 'quickTags', 'relatedTo')->makeVisible(
          ['idResource',
            'coachVisibility',
           'thumbnail',
           'studentVisibility',
           'isHTML',
           'studentIndex',
           'coachIndex',
           'isURL',
           'source',
           'path',
           'status',
           'updateTime',
           'isfolder',
           'parentResource',
           'idModifierUser',
           'idResourceType',
           'idCreatorUser',
           'idCreationCountry']
          ), 201);
  }

我的model是这个:

<?php
namespace Knotion;
use Illuminate'Database'Eloquent'Model;
class CTL_Resource extends Model  {
  protected $table = "CTL_Resource";
  protected $primaryKey = "idResource";
  public $incrementing = false;
  public $timestamps = false;
  public static $snakeAttributes = false;
  protected $hidden = [
        'coachVisibility', 'thumbnail', 'studentVisibility',
        'isHTML', 'studentIndex', 'coachIndex',
        'isURL', 'source', 'path',
        'status', 'updateTime', 'isfolder',
        'parentResource', 'idModifierUser', 'idResourceType',
        'idCreatorUser', 'idCreationCountry', 'user',
        'country', 'resource', 'coachVisibility',
        'studentVisibility', 'studentIndex', 'isHTML',
        'coachIndex', 'isURL', 'URL'
      ];
  protected $appends = [
    'creatorUser',
    'creationCountry',
    'resourceType'
  ];
  protected $fillable = [
    'coachVisibility',
    'studentVisibility',
    'studentIndex',
    'isHTML',
    'coachIndex',
    'isURL',
    'URL',
    'idResourceType',
    'productionKey',
    'title',
    'thumbnail',
    'description',
    'URL',
    'fileName',
    'extension',
    'minimumAge',
    'maximumAge'
  ];
  public function user() {
    return $this
      ->belongsTo('Knotion'OPR_User', 'idCreatorUser', 'idUser')
      ->select('idUser','name', 'lastName');
  }
  public function getCreatorUserAttribute() {
    return $this->user->name.' '.$this->user->lastName;
  }
  public function country() {
    return $this
      ->belongsTo('Knotion'CTL_Country', 'idCreationCountry', 'idCountry')
      ->select('idCountry', 'name');
  }
  public function getCreationCountryAttribute() {
    return $this->country->name;
  }
  public function resource()  {
    return $this
      ->belongsTo('Knotion'CTL_ResourceType', 'idResourceType', 'idResourceType')
      ->select('idResourceType', 'name');
  }
  public function getResourceTypeAttribute() {
    return $this->resource->name;
  }
  public function quickTags() {
    return $this
      ->belongsToMany('Knotion'CTL_QuickTag', 'CTL_Resource_has_QuickTags', 'idResource','idQuickTag');
  }
  public function tags() {
    return $this
      ->belongsToMany('Knotion'CTL_Tag','CTL_Resource_has_Tags', 'idResource', 'idTag');
  }
  public function relatedTo() {
    return $this
      ->belongsToMany('Knotion'CTL_RelatedTo', 'CTL_Resource_has_RelatedTo', 'idResource', 'idRelatedTo');
  }
  public function scopeSearch($query, $data) {
    if (trim($data) == "")
      return;
      $query
        ->orWhere('idResource', 'like', "%$data%")
        ->orWhere('title', 'like', "%$data%")
        ->orWhere('description', 'like', "%$data%")
        ->orWhere('minimumAge', 'like', "%$data%")
        ->orWhere('maximumAge', 'like', "%$data%")
        ->orWhere('fileName', 'like', "%$data%")
        ->orWhere('extension', 'like', "%$data%")
        ->orWhere('URL', 'like', "%$data%")
        ->orWhere('productionKey', 'like', "%$data%");
      $query->orWhereHas('user', function ($query) use ($data) {
        $query->where('name', 'like', "%$data%");
      });
      $query->orWhereHas('country', function ($query) use ($data) {
        $query->where('name', 'like', "%$data%");
      });
      $query->orWhereHas('resource', function ($query) use ($data) {
        $query->where('name', 'like', "%$data%");
      });
      $query->orWhereHas('quickTags', function ($query) use ($data) {
        $query->where('name', 'like', "%$data%");
      });
      $query->orWhereHas('tags', function ($query) use ($data) {
        $query->where('name', 'like', "%$data%");
      });
      $query->orWhereHas('relatedTo', function ($query) use ($data) {
        $query->where('name', 'like', "%$data%");
      });
    }
  public function scopeJoinRes($query) {
    $query
        ->join('OPR_User', 'OPR_User.idUser', '=', 'CTL_Resource.idCreatorUser')
        ->join('CTL_Country', 'CTL_Country.idCountry', '=', 'CTL_Resource.idCreationCountry')
        ->join('CTL_ResourceType', 'CTL_ResourceType.idResourceType', '=', 'CTL_Resource.idResourceType')
        ->select(
          'CTL_Resource.*',
          'OPR_User.name as creatorUser',
          'CTL_Country.country as creationCountry',
          'CTL_ResourceType.resourceType as resourceType'
        );
  }
}

感谢您与我分享您的时间和知识。:)

尝试在同步属性之前保存模型,这边:

$resource = new CTL_Resource($request->all());
$resource->save();
$resource->quickTags()->sync($request->quickTags);
$resource->tags()->sync($request->tags);
$resource->relatedTo()->sync($request->relatedTo);
相关文章: