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!

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

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错