如何在 Laravel 中将图像路径保存到数据库中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37583265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:55:40  来源:igfitidea点击:

How to save image path into database in Laravel?

phpimagelaravellaravel-5.2

提问by Chonchol Mahmud

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:

我想上传图像并在需要时从数据库中检索。我的代码可以完美地上传图像,但它的路径没有保存在数据库中。在数据库中它显示。这Null是我的代码:

    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

更新:Profile.php

class Profile extends Model
{
    protected $table = "profiles";
    public $fillable = ["firstName","lastName","middleName","DOB","gender","featuredProfile","email","phone","summary","profilePic"];
}

采纳答案by Hasibur Rahman Omi

In your code there should be like this:

在你的代码中应该是这样的:

$profile->profilePic = $fileName;

& then

& 然后

$profile->save();

回答by Bhupendra Mistry

1.How to save image path into database?

1.如何将图片路径保存到数据库中?

To save your full image path in a database field, the code is:

要将完整图像路径保存在数据库字段中,代码为:

$profile->profilePic = $destinationPath.$fileName;
$profile->save();


2.And how to retrieve image from database?


2.以及如何从数据库中检索图像?

In your view file using blade engine, write HTML code like this. In the image src you have to provide profilePic data as shown in the below HTML code:

在您使用刀片引擎的视图文件中,像这样编写 HTML 代码。在图像 src 中,您必须提供 profilePic 数据,如下面的 HTML 代码所示:

<img src="{{asset($profile->profilePic)}}"/>

回答by Abdul Hameed

try this

尝试这个

$path = $file->getRealPath();;
    $pos = strpos($path,'/public/');
    if ($pos !== false) {
        $path = substr($path, $pos + 1);
    }
    $input['file'] = $path;