0

i have this error when I try to send an array to my database table. i have this on my update method

$post = Post::findOrFail($id);
$post->PostTitle = $request->PostTitle;
$post->post = $request->post;

$tags = new Tag;

foreach ($request->tags as $tags) {

    $tag = Tag::create(['tag' => $tags]);

    if ($tag->save()) {
        $post->tags()->sync($tag->id);
    }
}

this is my html form

<div class="post-panel-3">
        <div class="form-group">
          <label for="postTags">Etiquetas: </label>
            <input type="text" name="tags[]" class="form-control" placeholder="Etiquetas" value="<?php if(isset($post->tags)){ foreach($post->tags as $tag); echo $tag->tag;} ?>" id="tagBox2">

I have ManyToMany relationship model, all run fine in create method but no on update. I think i miss something but not finded yet.

5
  • 2
    Your input name in your form is taggged, not tags. And the field is most likely malformed from the way you're creating it in your form. Commented Mar 13, 2017 at 17:34
  • I edit the input name, jut a typo. i dont understand what you said. i was try to changed variable name, but it doesn't work. Commented Mar 13, 2017 at 18:25
  • If you look at the source code of the form in the browser, you'll see your value is 12342236 if your post has the tags 1, 2, 3, 4, 22, and 36. You should probably put the tags field inside the foreach instead. Commented Mar 13, 2017 at 18:27
  • Also, you are using sync which means each time through the loop, you are going to erase all past tags you just saved for that post. You probably want to use attach instead. Commented Mar 13, 2017 at 19:02
  • Also if i use attach, the input is send a null value. Commented Mar 14, 2017 at 20:29

1 Answer 1

1

The sync() method takes an array as parameter. You are passing an object.

Create an array of IDs first, then pass it to the sync function.

$ids = [];
foreach ($request->tags as $tags) {
    $tag = Tag::create(['tag'=>$tags]);
    $ids[] = $tag->id;
}

//sync 
$post->tags()->sync($ids); //array of ids 
Sign up to request clarification or add additional context in comments.

11 Comments

I thought that my mistake was to pass an object instead of an array, but i saw that my input don't send any value to my controller, i try with all method, post, put, but nothing i don't get it.
View the source code of your form. Do you see any value in the input?
<input type="text" name="tags[]" class="form-control" value="etqiueta 1" placeholder="Etiquetas" id="tagBox2" tabindex="-1" style="position: absolute; left: -10000px;">
I will order my idea again, i want to attach new tags on post if people write on it, if not just leave the tags same as the post was created.
so request->tags should return 'etqiueta 1'. In your controller do dd($request->tags); and see what it displays.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.