태그를 posts 테이블과 동기화하려고합니다. 두 모델은 모델에서 서로에 대한 belongsToMany
관계가 있습니다. 그렇지 않으면 내가 태그에 대한 DB 항목을 작성, 태그는 DB에 포함 된 경우laravel synch 원인 BelongsToMany :: sync()는 배열 형식이어야합니다.
<input type="text" data-provide="typeahead" value="{{ Input::old('tags', $theTags) }} class="typeahead" data-items="1" name="tags" "/>
, 내가 확인 :
내보기는 나에게 태그를 자동으로 권장하고 이전 값을 얻기 위해 부트 스트랩 선행 입력 함께 :의
public function postEdit($postId = null)
{
$theTags=array();
$tags = explode(',', Input::get('tags'));
//check if tag exists in db
foreach ($tags as $key=>$value){
$dbtag = Tag::where('name', '=', $value)->first();
array_push($theTags, $dbtag);
//add to db
if(!$dbtag){
$dbtag = new Tag;
// Update the tag
$dbtag->name= e(ucwords($value));
$dbtag->slug = e(Str::slug($value));
$dbtag->save();
array_push($theTags, $dbtag);
}
}
// Update the blog post data
$post->title = e(Input::get('title'));
$author = Author::find(Input::get('author_id'));
// Was the blog post created?
if($author->posts()->save($post))
{
$post->categories()->sync(Input::get('categories'));
$post->tags()->sync(Input::get('tags'));
// Redirect to the new blog post page
return Redirect::to("admin/blogs/$postId/edit")->with('success', Lang::get('admin/blogs/message.update.success'));
}
}
나는 점점 오전 오류 :
Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::sync() must be of the type array, string given, called in /home/ytsejam/public_html/remaker/app/controllers/admin/BlogsController.php on line 273 and defined
당신은을 얻기 위해 저를 보여줄 수 태그를 올바르게 동기화 하시겠습니까?
ps : 다른 배열 $ theTags를 추가하려고했는데 array_push를 사용하고 있습니다. 내가
$post->tags()->sync($theTags);
으로하려고하면 불법 오프셋 오류가 점점 오전 :
나중에 자정 이후, 나는 내가 필요로 나누었다 동일한 솔루션에 온 array_push ($ theTags, $ dbtag-> id);를 수행합니다. 그렇지 않으면 db는 태그의 ID가 아닌 이름으로 채워집니다. 고맙습니다. – ytsejam