php - Laravel: JSON to post many to many relations Error: SQLSTATE[23000]: Integrity constraint violation -
i'm trying post
data json
, alright except when try send data many many
relationship.
(i'm using uuid
)
this error:
sqlstate[23000]: integrity constraint violation: 1048 column 'idresource' cannot null (sql: insert
ctl_resource_has_tags
(idresource
,idtag
) values (, 0119a3b2-04da-11e6-9079-6971fda2300c))
this json i'm trying post
tags
, quicktags
, relatedto
many many
relations, don't know how send or if i'm okey json or way i'm trying store data.
{ "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": [] }
this function in controller try store data:
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); }
and model
one:
<?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 creatoruser', 'ctl_country.country creationcountry', 'ctl_resourcetype.resourcetype resourcetype' ); } }
thank time , knowledge share me. :)
try save model before sync attributes, way:
$resource = new ctl_resource($request->all()); $resource->save(); $resource->quicktags()->sync($request->quicktags); $resource->tags()->sync($request->tags); $resource->relatedto()->sync($request->relatedto);
Comments
Post a Comment