Dunesmart's avatar

Image Upload and Saving in laravel 5.6

How can I save the image to a folder and the path to the database and retrieve the same in my view?

I have the following code in my controller;

if($request->hasfile('business_logo')) { $file = $request->file('business_logo'); $filename=time().$file->getClientOriginalName(); //$file->move(public_path().'/uploads/logos/', $filename); $file->move('uploads/logos/', $filename); }

                    $basic = Basic::create([

                        'business_name' => $request->business_name,
    
                        'tagline' => $request->tagline,
                
                        'about' => $request->about,
                
                        'location'=>$request->location,
    
                        'user_id' => auth()->id(),
    
                        'business_email' => $request->business_email,
    
                        'business_phone' => $request->business_phone,
    
                        'business_logo' =>  'uploads/logos/'. $filename,
    
                        'service_one' => $request->service_one,
    
                        'service_two' => $request->service_two,
    
                        'service_three' => $request->service_three
    
                        ]);

                        Session::flash('success', 'Page created successfully.');
    
                        return redirect()->back();

This successfully uploads the image to the public folder but shows nothing in the database. How do I fix this? Thanks in anticipation of your support.

0 likes
10 replies
ekhlas's avatar

hi change file upload code like this

if($request->hasfile('business_logo')) 
{ 
  $file = $request->file('business_logo');
  $extension = $file->getClientOriginalExtension(); // getting image extension
  $filename =time().'.'.$extension;
  $file->move('uploads/logos/', $filename);
}

and Store in database with name

$basic = Basic::create([

                        'business_name' => $request->business_name,
    
                        'tagline' => $request->tagline,
                
                        'about' => $request->about,
                
                        'location'=>$request->location,
    
                        'user_id' => auth()->id(),
    
                        'business_email' => $request->business_email,
    
                        'business_phone' => $request->business_phone,
    
                        'business_logo' =>  'uploads/logos/'. $filename,
    
                        'service_one' => $request->service_one,
    
                        'service_two' => $request->service_two,
    
                        'service_three' => $request->service_three
    
                        ]);

                        Session::flash('success', 'Page created successfully.');
    
                        return redirect()->back();

Solved

1 like
Dunesmart's avatar

Thank you @ Ekhlas. However, I'm still having the same issue. the image is uploaded to the /uploads/logos directory but the database is showing only 3 entries under busines_logo which are data seeded into the db.

All other columns are populated but business_logo is showing empty for all the 6 rows so far created.

Dunesmart's avatar

I don't quite understand what you mean but the file type is string in migration table and file in view. The 3 records uploaded via db Seeding show http:/localhost/uploads/logos/sifenBrand.jpg, http:/localhost/uploads/logos/sanatech.jpg, http:/localhost/uploads/logos/esterdonBrand.jpg,

Shawdow's avatar

use variable $basic save to the database

$basic = Basic::create([

                        'business_name' => $request->business_name,
    
                        'tagline' => $request->tagline,
                
                        'about' => $request->about,
                
                        'location'=>$request->location,
    
                        'user_id' => auth()->id(),
    
                        'business_email' => $request->business_email,
    
                        'business_phone' => $request->business_phone,
    
                        'business_logo' =>  'uploads/logos/'. $filename,
    
                        'service_one' => $request->service_one,
    
                        'service_two' => $request->service_two,
    
                        'service_three' => $request->service_three
    
                        ]);

              $basic->save();
        
             return redirect()->back()->with('success', 'Page created successfully.');

Dunesmart's avatar

Hi @ Shadow, Still the same issue, Every other field (including the slug I just added) saves except the image field.

This is my Seeder table that saves completely while sending from the form skips the image.

$t1 = 'Cashpira Payments'; $t2 = 'Sanatech Global'; $t3 = 'Sifen Technologies';

    $r1 = [
            
            'business_name' => $t1,
            'slug' => str_slug($t1),
            'tagline' =>'Inspiring innovations',
            'about' => 'Payment Solutions Company',
            'business_logo' => asset('uploads/cashPiraBrand.jpg'), 
            'category_id' => 1,
            'city_id' => 1,
            'location' => '4 Trnity House, Mabushi, Abuja',
            'business_email' => '[email protected]',
            'business_phone' => '07045321122',
            'service_one' => 'Payments Solutions',
            'service_two' => 'Mobile Money Transfer Solutions',
            'service_three' => 'Tax Solutions'
    ];

    $r2 = [
            
            'business_name' => $t2,            
            'slug' => str_slug($t2),
            'tagline' =>'Promoting Indigeous Solutions',
            'about' => 'Oil and Gas Company',
            'business_logo' => asset('uploads/sanatech.jpg'), 
            'category_id' => 4,
            'city_id' => 2,
            'location' => '43 Woji Housing Estate, Port Harcourt',
            'business_email' => '[email protected]',
            'business_phone' => '07055321127',
            'service_one' => 'Electrification',
            'service_two' => 'Telecoms Installation',
            'service_three' => 'Power Solutions'
    ];

    $r3 = [
    
            'business_name' => $t3,            
            'slug' => str_slug($t3),
            'tagline' =>'Powering Ideas and Innovation',
            'about' => 'Technology Company',
            'business_logo' => asset('uploads/sifenBrand.jpg'), 
            'category_id' => 5,
            'city_id' => 1,
            'location' => '43 Woji Housing Estate, Port Harcourt',
            'business_email' => '[email protected]',
            'business_phone' => '07067721129',
            'service_one' => 'Smart Power Solutions',
            'service_two' => 'Smart Software Applications',
            'service_three' => 'Business Innovation'
    ];

    
    Basic::create($r1);
    Basic::create($r2);
    Basic::create($r3);

my Controller

            if($request->hasfile('business_logo')) 
                { 
                $file = $request->file('business_logo');
                $extension = $file->getClientOriginalExtension(); // getting image extension
                $filename =time().'.'.$extension;
                $file->move('uploads/logos/', $filename);
                }

                    $basic = Basic::create([

                        'business_name' => $request->business_name,
    
                        'tagline' => $request->tagline,
                
                        'about' => $request->about,
                
                        'location'=>$request->location,
            
                        'business_email' => $request->business_email,
    
                        'business_phone' => $request->business_phone,
    
                        'business_logo' => 'uploads/logos/'. $filename,
    
                        'service_one' => $request->service_one,
    
                        'service_two' => $request->service_two,
    
                        'service_three' => $request->service_three,

                        'slug' => str_slug($request->business_name)
                        
    
                        ]);

                        $basic->save();
    
                        return redirect()->back()->with('success', 'Page created successfully.');

and in my view I have,

Business Logo

I quite appreciate your kind assistance.

zymawy's avatar

You Could Use store Method Well Hashed The Name And Automatically Move The Image Into Storage Folder With Second Parms You Can Choose The Public Disk To Sive It Publicly

    // Now  $business_logo Has Full Name And Hashed You Just Need To Save It In Database 
// Somthing Like This storage/mohammad.hadi/almhnds//qLpunKA6SEvPgqtS7SCyUr7r5TiB91UdfsQJQlzM.jpeg
     $business_logo= $request->file('business_logo')->store(
     'your/folder/to/save/the-image', 'public'
    );

Than Just Linked

    php artisan storage:link

If You Want To Retirvate The Image

<img src="{!! asset('storage/'. $photo->path ) !!}" class="img-fluid" alt="">
Dunesmart's avatar

Thank you @Zymawy,

The image is moved to the desired folder. The issue I am having is that the image column in the mysql database is empty while other fields are populated.

ActiveMonkey's avatar

Is the business_logo field in the fillable array in your model?

Dunesmart's avatar

Yes, it is. Otherwise it will be given me mass assignment error.

Please or to participate in this conversation.