, . , - .
, belongsTomany ( ), belongsTomany ( hasMany - , ). , Laravel .
( ) , ( ->tag()->sync(), ). ( laravel , post_id), . , , .
, "", , , "-", , . , tag post shoudl tags posts .
, :
class Post extends Eloquent
{
public function tags()
{
return $this->belongsToMany('Tag');
}
}
class Tag extends Eloquent
{
public function posts()
{
return $this->belongsToMany('Post');
}
}
class PostController extends BaseController
{
public function addPost()
{
$success = false;
DB::beginTransaction();
try {
$post = new Post;
$post->title = Input::get('post_title');
$post->content = Input::get('post_content');
if ($post->save()) {
$tag_ids = Input::get('tags');
$post->tags()->sync($tag_ids);
$success = true;
}
} catch (\Exception $e) {
}
if ($success) {
DB::commit();
return Redirect::back()->withSuccessMessage('Post saved');
} else {
DB::rollback();
return Redirect::back()->withErrorMessage('Something went wrong');
}
}
}
- , . - , , .