MajedAbdullah's avatar

How to upload multiple image in a single row?

Hello. I am looking for a solution here. I want to keep multiple images in a single row.

        if($request->hasFile('files')){
            foreach ($request->file('files') as $file) {
                $files =  $file->store('public');
                $fileName =  (explode('/',$files))[1];
                $host = $_SERVER['HTTP_HOST'];
                $projectImage = 'http://'.$host.'/storage/'.$fileName;
                DB::table('projects')->insert([
                    'project_name' => $addProjectName,
                    'project_des' => $addProjectDes,
                    'project_link' => $addProjectLink,
                    'project_image' => $projectImage,
                    'project_image1' => $projectImage,
                    'project_image2' => $projectImage,
                    'project_image3' => $projectImage,
                    'project_image3' => $projectImage,
                    'project_image5' => $projectImage,
                    'project_image6' => $projectImage,
                    'project_image7' => $projectImage,
                    'project_image8' => $projectImage,
                    'project_image9' => $projectImage,
                    'project_image10' => $projectImage,


                ]);
            }
        }

I want each picture to be inserted on the specific column. But in this case, it creates a new row for each image.

0 likes
3 replies
mvd's avatar

Hi @majedabdullah

Not tested but something like this?

if($request->hasFile('files')){
    $counter = 0;
    
    $project = [
        'project_name' => $addProjectName,
        'project_des' => $addProjectDes,
        'project_link' => $addProjectLink
    ];
    
    $host = $_SERVER['HTTP_HOST'];
    foreach ($request->file('files') as $file) {
        $files =  $file->store('public');
        $fileName =  (explode('/',$files))[1];        
        $projectImage = 'http://'.$host.'/storage/'.$fileName;
        
        if (!$counter) {
            $project['project_image'] = $projectImage;
        }
        else {
            $project['project_image' . $counter] = $projectImage
        }
        $counter++;
        
    }

    DB::table('projects')->insert($project);
}
Snapey's avatar

use spatie media library. It will make your life better and more fulfilled

Please or to participate in this conversation.