ene's avatar
Level 2

Array to string conversion

getting this error when im trying to upload a images ErrorException Array to string conversion

  protected $rules = [
        'category' => 'required|integer|exists:categories,id',
        'title' => 'required|min:4',
        'body' => 'required|min:4',
    ];

    public function createPost()
    {
        if (auth()->check()) {
            $this->validate(
                [
                    'images.*' => 'image|max:1024', // 1MB Max
                ]
            );
            $random =  str_pad(mt_rand(1,999999),6,'0',STR_PAD_LEFT);
            $post = Post::create([
                'user_id' => auth()->user()->id,
                'title' => $this->title,
                'category_id' => $this->category,
                'status_id' => $this->status,
                'body' => $this->body,
                'post_number' => $random,
                'slug' => Str::slug($this->title) ?? null,
                'image' => $this->images,
            ]);

            foreach ($this->images as $key => $image) {
                $this->images[$key] = $image->storeAs('posts');
            }

            $this->images = json_encode($this->images);

                session()->flash("message", "Featured image successfully uploaded");
0 likes
12 replies
sr57's avatar

You try to put an array to a single variable

Probably here

''' 'image' => $this->images, '''

sr57's avatar

@ene I can't read in your mind :-)

  1. Define what you want to do
  2. Define you model(s)
  3. Code
Snapey's avatar

when you designed the database, how did you think you would store multiple images in the images column?

ene's avatar
Level 2

@Snapey here is my database

 {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->onDelete();
            $table->foreignId('category_id');
            $table->foreignId('status_id');
            $table->bigInteger('post_number');
            $table->string('title');
            $table->longText('body');
            $table->string('image')->nullable();
            $table->string('slug');
            $table->boolean('featured')->default(0);

            $table->timestamps();
        });
    }
sr57's avatar

@ene With this structure you (want to) store ONE image per post , so don't try to upload more.

1 like
ene's avatar
Level 2

@sr57 how can one save more than one images to the database

1 like
ene's avatar
Level 2

@sr57 i have follow the steps but the image is not been saved to the database and i am not getting any error

 $post = Post::create([
                'user_id' => auth()->user()->id,
                'title' => $this->title,
                'category_id' => $this->category,
                'status_id' => $this->status,
                'body' => $this->body,
                'post_number' => $random,
                'slug' => Str::slug($this->title) ?? null,
                'filename' => $this->images,
            ]);

            foreach ($this->images as $key => $image) {
                $this->images[$key] = $image->storeAs('posts', str::random(30));
            }

            $this->images = json_encode($this->images);
ene's avatar
Level 2

@snapey @sinnbeck @sr57 this worked but is this the best to handle image upload for post

 foreach ($this->images as $key => $image) {
                $this->images[$key] = $image->storeAs('posts', str::random(30));
                PostImages::create([
                    'post_id' => $post->id,
                    'filename' => $image,
                ]);
            }

            $this->images = json_encode($this->images);

Please or to participate in this conversation.