php - laravel 更新功能不起作用

标签 php mysql laravel laravel-4

我想将图像保存到数据库

$img_data = file_get_contents(Input::file('imgInp'));
        $base64 = base64_encode($img_data);
        $admin = Admin::find(25);
        $admin->picture = $base64;
        $admin->update();
        echo $base64;

->update() 函数似乎没有对我的数据库执行任何操作,因为尽管 $base64 图片字段始终为 NULL有数据。 mysql 数据库中的列类型是 longblob 并且我已经尝试执行 ->save() 但仍然没有任何内容被保存到数据库中。请帮忙

最佳答案

图像上传到临时文件夹中,因此:

#Get the path to the uploaded img
$filePath = Input::file('imgInp')->getRealPath();
$fileName = Input::file('imgInp')->getClientOriginalName();
$extension = Input::file('imgInp')->getClientOriginalExtension();

#Lets add the extension to the file name
$fileNameWithExtension = $fileName . '.' . $extension;

#Create the folder img/uploaded on your public folder!
$destination = public_path('img/uploaded/' . $fileNameWithExtension);

#Copy the img to your desired destination
#copy ( $filePath, $destination );

#Instead of the copy function you can use this laravel function
Input::file('imgInp')->move($destination);

#Save on database the path to your img

$admin = Admin::find(25);
$admin->picture = $destination;
$admin->update();

我希望它能起作用。没试过

然后,如果您想显示您的图像,只需执行以下操作:

$admin = Admin::find(25);

#Pass the variable to your view and them, if you use blade templating system:
<img src="{{asset($admin->picture)}}">

#If you are not using blade do it this way
<img src="<?php echo asset($admin->picture); ?>">

关于php - laravel 更新功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24431988/

相关文章:

php - jQuery 简单表单提交无需重新加载

php - 更短的 POST 验证方式?

php - MySQLi CREATE 表查询不工作

mysql - Laravel 中如何通过数据透视表获取相关模型?

Laravel:belongsToMany() 没有获取多对多表中的字段

PHP/在子目录中正确使用包含

python - 如何查询描述非规范化表的多个 Django 模型

mysql - 从与外键的关系开始

php - guzzlehttp 在 Laravel 项目上抛出错误 - strtolower() : Argument #1 ($string) must be of type string, int 给出

php - laravel Auth::login($user) 总是返回 401 Unauthorized