dane5890's avatar

Array To String Conversion Error

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?

0 likes
7 replies
dane5890's avatar

@sidneygijzen Thanks for the response, but after implementing your changes, I still get the name error

furqanDev's avatar

Check what you are getting in request object from view dd($request->category_id)

dane5890's avatar

@furqanDev Funny, when I do I die dump on the create view page, I get Undefined variable: request. I made use to pass in the controllers and models through a php block like so

@php

use App\Models\HairBlog;
use App\Models\HairBlogCategory;
use App\Models\HairBlogSubCategory;
use App\Models\HairBlogSubSubCategory;
use App\Models\HairBlogSubSubSubCategory;
use App\Http\Controllers\HairBlogController;

dd($request->category_id);

@endphp

When I use the request function in the HairBlogController, it recognizes the request, but I get the array to string error.

furqanDev's avatar

@dane5890 and obviously you need to dd($request) in your controller. You are sending request to a controller not a view so, you catch it in controller.

dane5890's avatar

@furqanDev It appears that the problem is that the dd($request) isn't doing anything. It should have pulled up some information, instead, I get the regular screen

Please or to participate in this conversation.