2013-11-24 2 views
0

태그를 posts 테이블과 동기화하려고합니다. 두 모델은 모델에서 서로에 대한 belongsToMany 관계가 있습니다. 그렇지 않으면 내가 태그에 대한 DB 항목을 작성, 태그는 DB에 포함 된 경우laravel synch 원인 BelongsToMany :: sync()는 배열 형식이어야합니다.

컨트롤러 IIN
<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); 

으로하려고하면 불법 오프셋 오류가 점점 오전 :

답변

2

Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::sync() must be of the type array, string given

당신이 문자열 sync() 호출하기 때문에 그 오류이며, 배열되어야한다 :

// Input::get('tags') is a string, this will throw an error 
$post->tags()->sync(Input::get('tags')); 

이제 다른 오류가 발생했습니다.

ps: I tried to add another array $theTags ,I am using array_push on them . when I try with $post->tags()->sync($theTags); I am getting illegal offset error.

나는 두 번 array_push()를 호출하기 때문에 당신의 $theTags 배열에 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(); 

    //add to db 
    if (! $dbtag) { 
     $dbtag = new Tag; 
     // Update the tag 
     $dbtag->name= e(ucwords($value)); 
     $dbtag->slug = e(Str::slug($value)); 
     $dbtag->save(); 
    } 

    // Now that you have checked if it's null and created it 
    // you can add it safely to the array 
    array_push($theTags, $dbtag);  
} 

을 한 후, 다시 배열 sync()를 호출하려고 :

//check if tag exists in db   
foreach ($tags as $key=>$value) { 
    $dbtag = Tag::where('name', '=', $value)->first(); 
    array_push($theTags, $dbtag); // if $dbtag is null, it's being added too 
... 

나는이 함께 노력할 것이라고 $post->tags()->sync($theTags);

+0

나중에 자정 이후, 나는 내가 필요로 나누었다 동일한 솔루션에 온 array_push ($ theTags, $ dbtag-> id);를 수행합니다. 그렇지 않으면 db는 태그의 ID가 아닌 이름으로 채워집니다. 고맙습니다. – ytsejam

관련 문제