karimali1337's avatar

Array to String Conversion

I'm trying to store multi image in another table and it gives me array to string conversion

Controller

public function store(PermitRequest $request)
    {
        $data = $request->all();
        $data['created_by'] = auth()->user()->id;
        if ($request->type == null) {
            $data['type'] = 'regular';
        }
        foreach ($request->risk_attachement as $uploadImage) {

            // if ($request->hasFile('risk_attchament')) {
                $fileName = upload_image($uploadImage, '_', 'RiskAttchament');
                Attachement::create([
                    'type' => 'risk_attachement',
                    'permit_id' => $id,
                    'file_path' => $fileName,
                ]);
            // }

        }

Upload Image Function

if (!function_exists('upload_image')) {
    function upload_image($file, $prefix, $folder)
    {
        if ($file) {
            $files = $file;
            $imageName =$prefix.time()."_".$files->getClientOriginalName();
            $image = "storage/" . $folder . "/" . $imageName;
            $files->move(public_path('storage' . "/" . $folder), $imageName);
            $getValue = $image;
            return $getValue;
        }
    }
}

Attachement Model

class Attachement extends Model
{
    use HasFactory;
    protected $fillable = [
        'type',
        'permit_id',
        'file_path',
    ];
}

HTML Input

   <div class="form-group">
 <label>Upload Files</label>
<input type="file" class="form-control" multiple name="risk_attachements[]"
 placeholder="Example input placeholder">
 </div>
</div>

0 likes
6 replies
thinkverse's avatar

In your store method, you are using risk_attachement but in your view, you have risk_attachements, it's missing an s in the store method. Could that possibly be the issue? If you provide the actual error that could give better insights.

thinkverse's avatar

@karimali1337 Then it would help if you included the actual error instead of just the code. It's hard to guess where the issue is without the exception. The exception will provide you with a stack trace containing the file and line where you are trying to convert an array to a string. That would be of big help for us who are trying to help you solve this issue.

vybeauregard's avatar

dump $files so you can see what you're dealing with. It's likely an array and you'll need to process each uploaded file separately.

karimali1337's avatar
karimali1337
OP
Best Answer
Level 2

The Solution

        if (isset($request->risk_attachements) && count($request->risk_attachements) > 0) {
            for ($i = 0; $i < count($request->risk_attachements); $i++) {
                $fileName = upload_image($request->risk_attachements[$i], '_', 'RiskAttchament');
                Attachement::create([
                    'type' => 1,
                    'permit_id' => $request->id,
                    'file_path' => $fileName,
                ]);
            }
        }

Please or to participate in this conversation.