Laravel Eloquent Relations: Saving two foreign keys at once -


i have resource , post model. resource can either image (the address of image) or text. entry in posts table relate 2 of entries on resources model.enter image description here

hence, posts uses 2 foreign keys, both refer resources.id column. have found this related post doesn't how presist attribute without getting foreign key integrity constraint violation.

these have tried far no avail:

        //$post->resources($image);//doesn't add image_id         //$post->resources($text);//or text_id, doesn't work, no type-hinting          //$image->post()->associate($post);//tries save resources         //$text->post()->associate($post);//and not post          //$post->resources()->push($image);// push save         //$post->resources()->push($text);// save updated         //$post->push($text);// relations 

i know problem codes/attemps above; fyi.

the frustrating thing $post->image()->save($image) try insert resources table , not posts table. furthermore, inverse, $image->post()->save($image) missing text_id.

i have tried: $post->savemany(['text'=>$text,'image'=>$image]); , doesn't anything.

if require anymore details, please leave comment. p.s. don't have enough points ask in so's related posts comments, otherwise wouldn't start new question.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -