I have a blog app that I need to add tags on post. I have an input field, post model, tag model and 3 tables on database: posts
, tags
and tag_post
with ManyToMany relationship.
When I try to add new tag on table, it send to tag_post table and not tag table. I did trying change my model and controller but it worse.
Post Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $filliable = ['PostTitle', 'post', 'slug'];
public function categories()
{
return $this->BelongsToMany('App\Categories', 'category_post');
}
public function tags()
{
return $this->BelongsToMany('App\Tag', 'tag_post');
}
}
Tag Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $filliable = ['tag'];
public function posts()
{
return $this->BelongsToMany('App\Post', 'tags_post');
}
}
PostController
public function store(Request $request)
{
$post = new Post;
$post->user_id = auth::id();
$post->PostTitle = $request->PostTitle;
$post->post = $request->post;
$post->slug = $request->slug;
$post->status = '1';
$post->save();
$post->categories()->attach($request->categories);
$tags = new Tag;
foreach($tags as $tag){
$tags->tag = $request->tags;
if($tags->save()){
$post->tags()->attach($request->tags);
}
}
return redirect('blog')->with('messageSuccess', '¡Tu entrada se ha creado Exitosamente!');
}
Form
<div class="post-panel-3">
<div class="form-group">
<label for="postTags">Etiquetas: </label>
<input type="text" name="tags[]" value=" " class="form-control" placeholder="Etiquetas" id="tagBox">
</div>
I edit the if statement but i have this error when i save tags
Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\laragon\www\asiviajo-app\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 665
via Johanng1993