I want to insert a Post record with multiple tags so here's my code in Post@store:
$post = Post::create(array(
'title' => $request->title,
'body' => $request->body,
'user_id' => Auth::id(),
));
if($post && $request->tags)
{
$tagNames = explode(',', $request->tags);
$tagIds = [];
foreach($tagNames as $tagName)
{
$tag = Tag::firstOrCreate(['name'=>$tagName]);
if($tag)
{
$tagIds[] = $tag->id;
}
}
$post->tags()->attach($tagIds);
}
but it give me an errors "Call to a member function attach() on null"
. when i'm checked in mysql the tags is already in there but i can't find any entry on my post_tag tables.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2AQ2JTs
via IFTTT
Aucun commentaire:
Enregistrer un commentaire