duanchi0883649 2017-03-08 17:41
浏览 85
已采纳

保存数据并在Laravel中同时附加

I have a blog app that I need to add tags on post. I have an input field, post model, tag model and 3 tables on database: posts, tags and tag_post with ManyToMany relationship.

When I try to add new tag on table, it send to tag_post table and not tag table. I did trying change my model and controller but it worse.

Post Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

  protected $filliable = ['PostTitle', 'post', 'slug'];

  public function categories()
  {
    return $this->BelongsToMany('App\Categories', 'category_post');
  }

  public function tags()
  {
    return $this->BelongsToMany('App\Tag', 'tag_post');
  }

}

Tag Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    protected $filliable = ['tag'];


    public function posts()
    {
      return $this->BelongsToMany('App\Post', 'tags_post');
    }
}

PostController

public function store(Request $request)
{

    $post = new Post;
    $post->user_id = auth::id();
    $post->PostTitle = $request->PostTitle;
    $post->post = $request->post;
    $post->slug = $request->slug;
    $post->status = '1';
    $post->save();
    $post->categories()->attach($request->categories);

      $tags = new Tag;
    foreach($tags as $tag){
    $tags->tag = $request->tags;
    if($tags->save()){
      $post->tags()->attach($request->tags);
    }
  }

    return redirect('blog')->with('messageSuccess', '¡Tu entrada se ha creado Exitosamente!');
}

Form

<div class="post-panel-3">
            <div class="form-group">
              <label for="postTags">Etiquetas: </label>

              <input  type="text" name="tags[]" value=" " class="form-control" placeholder="Etiquetas" id="tagBox">

            </div>

I edit the if statement but i have this error when i save tags Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\laragon\www\asiviajo-app\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 665

  • 写回答

1条回答 默认 最新

  • douqiang5933 2017-03-09 00:41
    关注

    Try to save your tag before attaching it to a post.

    Instead of:

    ...
    if ($tag) {
        // Do your stuff
    }
    

    do this:

    ...
    if ($tag->save()) {
        // Do your stuff
    }
    

    By doing so, the tag is going to be saved in the tag table in your database. After that, in your if statement, your are going to attach the post to that tag you just saved.

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?