laravel synch Ursachen BelongsToMany::sync() muss vom Typ array, string given
Ich versuche, synch meine tags mit der posts-Tabelle. Zwei Modelle haben belongsToMany
Beziehungen zu einander in das Modell.
Meiner Sicht ist mit bootstrap typeahead, um autosuggest mich tags und Holen Sie sich die alten Werte:
<input type="text" data-provide="typeahead" value="{{ Input::old('tags', $theTags) }} class="typeahead" data-items="1" name="tags" "/>
iin der controller, ich überprüfen, ob ein tag-ist in der db, sonst kann ich erstellen db-Eintrag für den tag:
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'));
}
}
Bin ich immer Fehler von :
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
Können Sie mir zeigen, um die korrekte Synchronisierung der tags ?
ps: ich habe versucht, fügen Sie ein weiteres array $theTags ,ich bin mit array_push auf Sie . wenn ich versuche mit
$post->tags()->sync($theTags);
Ich bin immer illegal offset-Fehler:
Du musst angemeldet sein, um einen Kommentar abzugeben.
Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::sync() must be of the type array, string given
Fehler ist, weil Sie anrufen
sync()
mit einem string, und es sollte ein array sein:Nun, die anderen Fehler.
Ich denke, dieser Fehler ist passiert, weil Sie mit dem hinzufügen
null
Werte der$theTags
array, da Sie mit der Berufungarray_push()
zweimal.Ich würde versuchen, mit diesem:
und dann versuchen, zu nennen
sync()
mit dem array wieder:$post->tags()->sync($theTags);