3

I'm trying to store multiple images in the database and also show them in the view. I have products table and images table which are related, in the images table I have a foreign key named(image). So far am getting this error "Array to string conversion"

Here are the codes

Controller

  public function store(Request $request) 
  { 

    $formInput=$request->all();
    $image=array();
    if($files=$request->file('image')){
        foreach($files as $file){
            $name=$file->getClientOriginalName();
            $file->move('images',$name);
            $image[]=$name;

        }

    }
          product::create(array_merge($formInput,
       ['user_id' => Auth::user()->id,
       'image' => $image

    ])); 
    return redirect()->back(); 

Blade

  <input type="file" name="image[]" multiple class="form-control">

Product.php

   public function products()
 {
    return $this->belongsTo('App\Images', 'image');
  }

Images.php

   public function images()
   {
     return $this->hasMany(Product::class, 'image');
    }
16
  • 1
    What is the error you're experiencing? Commented Jun 28, 2019 at 10:47
  • The problem is it stores one image I wanted to store multiple@SimonC Commented Jun 28, 2019 at 10:48
  • Okay, that's the behaviour you're experiencing and the expected behaviour is different. Is there an error? An exception, error code, etc? What does Request look like? Is the Request object the same as the HTTP request? We need more info. Commented Jun 28, 2019 at 10:50
  • This is ther error am getting "Array to string conversion" @SimonC Commented Jun 28, 2019 at 10:54
  • Can you please tell where you are getting "Array to string conversion" this error ? Commented Jun 28, 2019 at 11:30

1 Answer 1

2

As of my Knowledge there is no bug in the file upload but While You are trying to Store

Like this or may by object oriented

it will be a bug

  $CreateArray = array_merge($request->all(), [
    'image' => $image
                ]);

                Model::create( $CreateArray);

So Since if you are uploading the Multiple files and You will get the array of file names but you can't store it as a array in databse so

$CreateArray = array_merge($request->all(), [
    'image' => json_encode($image)
                ]);

                Model::create( $CreateArray);

EDITED

public function store(Request $request) 
  { 

    $formInput=$request->all();
    $image=array();
    if($files=$request->file('image')){
        foreach($files as $file){
            $name=$file->getClientOriginalName();
            $file->move('images',$name);
            $image[]=$name;

        }

    }
          product::create(array_merge($formInput,
       [
'user_id' => Auth::user()->id,
        'image' => json_encode($image)

    ])); 
    return redirect()->back(); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.