Hey, putting together a blog in Laravel 8. I'm trying to sync new blog creation with some categories that I created. I can easily create categories, sub categories, and blogs independently of each other, but when I try to sink a blog with categories, I get the
Array To String Conversion Error
Here is my controller code
public function Store(Request $request) {
$input = $request->all();
$blog = HairBlog::create($input);
// $blog = new HairBlog();
// $blog->title = $request->title;
// $blog->blog_body = $request->blog_body;
// $blog->save();
if($request->category_id) {
$blog->category()->sync($request->category_id);
}
I've created the relationships in my models like so
class HairBlog extends Model
{
use HasFactory;
use SoftDeletes;
protected $connection = 'salon-blogs';
protected $dates = ['deleted_at'];
protected $guarded = [];
public function category() {
return $this->belongsToMany(HairBlogCategory::class);
}
public function subcategory() {
return $this->belongsToMany(HairBlogSubCategory::class);
}
public function subsubcategory() {
return $this->belongsToMany(HairBlogSubSubCategory::class);
}
public function subsubsubcategory() {
return $this->belongsToMany(HairBlogSubSubSubCategory::class);
}
//
class HairBlogCategory extends Model
{
use HasFactory;
use SoftDeletes;
protected $connection = 'salon-blogs';
protected $dates = ['deleted_at'];
protected $guarded = [];
public function blog() {
return $this->belongsToMany(HairBlog::class);
}
In my blog,create view I've written the code in this manner
<div class="form-group form-check form-check-inline">
@foreach($categories as $category)
<input multiple="multiple " type="checkbox" name="category_id[ ]" value="{{ $category->id }}" class="form-check-input">
<label class="form-check-label">{{ $category->name }}</label>
@endforeach
</div>
Can anyway understand what the problem is?