Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save image path into database in Laravel?

I want to upload an image and retrieve in from database when i need this.My code is perfectly working to upload image but it's path did not save in database.In database it's showing Null.Here is my code:

    if ($request->hasFile('profilePic')) {
        $file = array('profilePic' => Input::file('profilePic'));
        $destinationPath = 'img/'; // upload path
        $extension = Input::file('profilePic')->getClientOriginalExtension(); 
        $fileName = rand(11111,99999).'.'.$extension; // renaming image
        Input::file('profilePic')->move($destinationPath, $fileName);
    }else{
        echo "Please Upload Your Profile Image!";
    }
    $profile->save();

My Question:

  • How to save image path into database?
  • And how to retrieve image from database?

Updated: Profile.php

class Profile extends Model
{
    protected $table = "profiles";
    public $fillable = ["firstName","lastName","middleName","DOB","gender","featuredProfile","email","phone","summary","profilePic"];
}
like image 798
Chonchol Mahmud Avatar asked Nov 28 '25 14:11

Chonchol Mahmud


1 Answers

In your code there should be like this:

$profile->profilePic = $fileName;

& then

$profile->save();
like image 71
Hasibur Rahman Omi Avatar answered Dec 01 '25 03:12

Hasibur Rahman Omi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!