gpt4 book ai didi

laravel-4 - 如何在Laravel中保存多对多态关系中的条目?

转载 作者:行者123 更新时间:2023-12-03 13:08:30 24 4
gpt4 key购买 nike

我有一个组织模型和一个标签模型。我想将标签与组织相关联。我的数据库表和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'))
}}
...

... $ tag_options来自我的routes.php文件...
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!');
});

现在,Input::get('tags')只是标签ID的数组,形式为
["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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com