作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个组织模型和一个标签模型。我想将标签与组织相关联。我的数据库表和Eloquent模型设置如下:
org
id - integer
name - string
...
tags
id - integer
name - string
taggables
id - integer
taggable_id - integer
taggable_type - string
// app/models/Org.php
class Org extends Eloquent
{
protected $table = "org";
...
public function tags()
{
return $this->morphToMany('Tag', 'taggable');
}
}
// app/models/Tag.php
class Tag extends Eloquent
{
protected $table = "tags";
public $timestamps = false;
public function org()
{
return $this->morphedByMany('Org', 'taggable');
}
}
...
{{ Form::select('tags[]', $tag_options, null, array(
'multiple',
'data-placeholder' => 'Select some tags'))
}}
...
View::composer('*', function($view)
{
$tags = Tag::all();
if(count($tags) > 0)
{
$tag_options = array_combine($tags->lists('id'),
$tags->lists('name'));
}
else
{
$tag_options = array(null, 'Unspecified');
}
$view->with('tag_options', $tag_options);
});
Route::put('org/{org}', function(Org $org){
$org->description = Input::get('description');
$org->website = Input::get('website');
$org->tags = Input::get('tags');
$org->save();
return Redirect::to('org/'.$org->id)
->with('message', 'Seccessfully updated page!');
});
["1","6","8"]
Route::put('org/post/{org}', function(Org $org){
$comment = new Comment;
$comment->user_id = Auth::user()->id;
$comment->body = Input::get('body');
$comment->commentable_id = $org->id;
$comment->commentable_type = 'Org';
$comment->save();
return Redirect::to('org/'.$org->id)
->with('message', 'Seccessfully posted comment!');
});
最佳答案
您可以为此使用所有belongsToMany
方法,因为多态多对多扩展了该关系:
// I would call that relation on tag in plural 'entities()' to be more accurate
$tag->entities()->save(new or existing model, array of pivot data, touch parent = true) (used on existing model)
$tag->entities()->saveMany(array of new or existing models, array of arrays with pivot data)
$tag->entities()->attach(existing model / id, array of pivot data, touch parent = true)
$tag->entities()->sync(array of ids, detach = true)
$tag->entities()->updateExistingPivot(pivot id, array of pivot data, touch)
$tag = Tag::first();
$entity = Entity::find(10);
// save() works with both newly created and existing models:
$tag->entities()->save(new Entity(...));
$tag->entities()->save($entity);
// saveMany() like above works with new and/or existing models:
$tag->entities()->saveMany([$entity, new Entity(...)]);
// attach() works with existing model or its id:
$tag->entities()->attach($entity);
$tag->entities()->attach($entity->id);
// sync() works with existing models' ids:
$tag->entities()->sync([1,5,10]); // detaches all previous relations
$tag->entities()->sync([1,5,10], false); // does not detach previous relations, attaches new ones skipping existing ids
Route::put('org/{org}', function(Org $org){
$org->description = Input::get('description');
$org->website = Input::get('website');
$org->save();
$org->tags()->sync(Input::get('tags'));
// or if you don't want to detach previous tags:
// $org->tags()->sync(Input::get('tags'), false);
return Redirect::to('org/'.$org->id)
->with('message', 'Seccessfully updated page!');
});
关于laravel-4 - 如何在Laravel中保存多对多态关系中的条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23896031/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!