Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中将图像路径保存到数据库中?_Php_Image_Laravel_Laravel 5.2 - Fatal编程技术网

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

Php 如何在Laravel中将图像路径保存到数据库中?,php,image,laravel,laravel-5.2,Php,Image,Laravel,Laravel 5.2,我想上载一个图像,并在需要时从数据库中检索。我的代码可以完美地上载图像,但其路径未保存在数据库中。在数据库中,它显示为Null。以下是我的代码: if ($request->hasFile('profilePic')) { $file = array('profilePic' => Input::file('profilePic')); $destinationPath = 'img/'; // upload path $ext

我想上载一个图像,并在需要时从数据库中检索。我的代码可以完美地上载图像,但其路径未保存在数据库中。在数据库中,它显示为
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();
我的问题:

  • 如何将图像路径保存到数据库中
  • 如何从数据库中检索图像
更新:Profile.php

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

在您的代码中应该有如下内容:

$profile->profilePic = $fileName;
&然后

$profile->save();
试试这个

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

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

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

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

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

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

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

你能发布你的个人资料模型文件吗?你不能用$profile->image\u location=$destinationPath./'.$filename;@Brett之类的东西吗?我已经更新了我的问题。