Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.2中的简单标签系统_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel 5.2中的简单标签系统

Php Laravel 5.2中的简单标签系统,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在我的laravel项目中制作简单的标签功能。我已经制作了表标签 id | tag 我还有表项 id | title | description 以及第三个表item_tag,它将保存item的id和tag 在我的项目模型中,我添加了这个关系 public function tags() { return $this->belongsToMany('App\Tag', 'item_tag'); } 以及标签模型中的关系 public function itemTags(

我正在我的laravel项目中制作简单的标签功能。我已经制作了表
标签

id | tag
我还有表

id | title | description
以及第三个表
item_tag
,它将保存
item
的id和
tag

在我的项目模型中,我添加了这个关系

public function tags() {

    return $this->belongsToMany('App\Tag', 'item_tag');
}
以及标签模型中的关系

public function itemTags() {

    return $this->belongsToMany('App\Item', 'item_tag');

}
项目创建控制器具有用于视图的
create()
,以及用于存储项目的
store()

public function create(){

   $allTags = Tag::all();
   return view('items.create', compact('allTags'));       
}

public function store( ItemRequest $request ){

    $item = new Item;
    $item->title = $request['title'];
    $item->description = $request['description'];        
    $item->save();

    return redirect()->route('items');       
}
在表单标签上选择多个标签(不确定这样做是否正确)


通常,您希望从项目中分离所有标记,并仅附加用户在表单中添加的标记。在这种情况下,请使用方法

sync方法接受要放置在中间表上的ID数组。不在给定数组中的任何ID都将从中间表中删除。因此,此操作完成后,中间表中将只存在给定数组中的ID

例如:

$item = new Item;
$item->title = $request['title'];
$item->description = $request['description'];        
$item->save();

// If some tags are new, you should create them here first and get their IDs from a DB.

$item->tags()->sync($request->tags);

哇,那很容易。这么简单,我错过了。现在,pivot表用标记更新。关于第二个问题当前我使用此查询显示单个项:
$item=item::find($id)。现在如何抓取标签?@VLS使用即时加载
$item=item::with('tags')->find($id)再次感谢,但你知道为什么当我试图在页面上显示它们时,
$item->tag
$item->tags
返回
NULL
吗?它返回
NULL
我在
dd($item)
上看到它。。我已经更新了我的问题,我在那里看到了什么。1个标签。这是正确的,因为我在透视表中有一个标记用于此项
  #relations: array:3 [▼
    "tags" => Collection {#330 ▼
      #items: array:1 [▼
        0 => Tag {#329 ▼
          #table: "tags"
          #primaryKey: "id"
          #fillable: array:1 [▶]
          #connection: null
          #keyType: "int"
          #perPage: 15
          +incrementing: true
          +timestamps: true
          #attributes: array:10 [▼
            "id" => 5
            "tag" => "testTag"
            "title" => null
            "subtitle" => null
            "page_image" => null
            "meta_description" => null
            "layout" => null
            "reverse_direction" => null
            "created_at" => "2017-01-25 06:52:59"
            "updated_at" => "2017-01-25 06:52:59"
          ]
        }
      ]
    }
  ]
$item = new Item;
$item->title = $request['title'];
$item->description = $request['description'];        
$item->save();

// If some tags are new, you should create them here first and get their IDs from a DB.

$item->tags()->sync($request->tags);