【发布时间】:2014-11-11 01:46:51
【问题描述】:
我是 Laravel 的新手。我从 github 代码修改了启动应用程序,以将给定的帖子详细信息保存到数据库中。 但面临以下2个问题
- 在逗号之前输入的第一个值仅插入到数据库中。我认为因为在关系表中只插入一条记录。
- 在编辑帖子中,类别显示为 json 结构
我已经为多对多关系建立了一个数据库。
帖子、类别和categories_post
创建了一个表单来添加/编辑帖子
<form class="form-horizontal" method="post" action="@if (isset($post)){{ URL::to('admin/blogs/' . $post->id . '/edit') }}@endif" autocomplete="off">
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<!-- ./ csrf token -->
<!-- Tabs Content -->
<div class="tab-content">
<!-- General tab -->
<div class="tab-pane active" id="tab-general">
<!-- Post Title -->
<div class="form-group {{{ $errors->has('title') ? 'error' : '' }}}">
<div class="col-md-12">
<label class="control-label" for="title">Post Title</label>
<input class="form-control" type="text" name="title" id="title" value="{{{ Input::old('title', isset($post) ? $post->title : null) }}}" />
{{{ $errors->first('title', '<span class="help-block">:message</span>') }}}
</div>
</div>
<div class="form-group {{{ $errors->has('categories') ? 'error' : '' }}}">
<div class="col-md-12">
<label class="control-label" for="categories">Categories</label>
<input class="form-control" type="text" name="categories" id="categories" value="{{{ Input::old('categories', isset($post) ? $post->categories : null) }}}" />
{{{ $errors->first('categories', '<span class="help-block">:message</span>') }}}
</div>
</div>
<!-- ./ post title -->
<!-- Content -->
<div class="form-group {{{ $errors->has('content') ? 'has-error' : '' }}}">
<div class="col-md-12">
<label class="control-label" for="content">Content</label>
<textarea class="ckeditor" name="content" value="content" rows="10">{{{ Input::old('content', isset($post) ? $post->content : null) }}}</textarea>
{{{ $errors->first('content', '<span class="help-block">:message</span>') }}}
</div>
</div>
<!-- ./ content -->
</div>
<!-- ./ general tab -->
</div>
<!-- ./ tabs content -->
<!-- Form Actions -->
<div class="form-group">
<div class="col-md-12">
<element class="btn-cancel close_popup">Cancel</element>
<button type="reset" class="btn btn-default">Reset</button>
<button type="submit" class="btn btn-success">Update</button>
</div>
</div>
<!-- ./ form actions -->
</form>
这是控制器代码
// Start transaction!
DB::beginTransaction();
$title = Input::get('title');
$categoriesString = Input::get('categories');
$categories = explode(',', $categoriesString);
$postDetails = $this->post->findByTitle($title);
if($postDetails)
{
return Redirect::to('blog/create')->withInput()->withErrors(array('title' => 'Title already exists'));
} else {
// Update the blog post data
$this->post->title = $title;
$this->post->slug = Str::slug(Input::get('title'));
$this->post->content = Input::get('content');
}
try
{ // Validate, then create if valid
$newPost = $this->post->save();
} catch(ValidationException $e)
{
// Rollback and then redirect
// back to form with errors
DB::rollback();
return Redirect::to('admin/blogs/create')->with('error', Lang::get('admin/blogs/messages.create.error'));
} catch(\Exception $e)
{
DB::rollback();
throw $e;
}
以逗号分隔的类别与表格
$categoriesString = Input::get('categories');
$categories = explode(',', $categoriesString);
然后检查该类别是否已存在于数据库中。如果存在,则获取它的 id 并附加到新数组。
$categoryClass = new Categories();
$categoryIds = array();
foreach($categories as $category)
{
try {
// Validate, then create if valid
$categoryName = strtolower($category);
$categoryDetails = $categoryClass->findByName($categoryName);
if($categoryDetails)
{
$categoryId = $categoryDetails->id;
} else {
$categoryClass->name = $category;
$categoryClass->save();
$categoryId = $categoryClass->id;
}
} catch(ValidationException $e)
{
// Rollback and then redirect
// back to form with errors
DB::rollback();
return Redirect::to('blog/create')->with('error', Lang::get('blog/messages.create.error'));
} catch(\Exception $e)
{
DB::rollback();
throw $e;
}
// Push category id into category id's array
$categoryIds[] = $categoryId;
}
//Then sync the categories with new categories
$this->post->categories()->sync($categoryIds);
// Commit
DB::commit();
return Redirect::to('blog/' . $this->post->id . '/edit')->with('success', Lang::get('blog/messages.create.success'));
POST 模型
/**
* Get the post's categories.
*
* @return array
*/
public function categories()
{
return $this->belongsToMany('Categories');
}
类别模型
/**
* Get the Categories's posts.
*
* @return array
*/
public function posts()
{
return $this->belongsToMany('Post');
}
请有人帮我解决这个问题。
【问题讨论】:
标签: php mysql laravel laravel-4