laravel更新功能不起作用

时间:2014-06-26 13:33:37

标签: 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()函数似乎对我的数据库没有任何作用,因为尽管NULL有数据,但图片字段始终是$base64。 mysql数据库中的列类型是longblob,我已经尝试->save(),但仍然没有将任何内容保存到数据库中。请帮忙

1 个答案:

答案 0 :(得分:2)

图像上传到临时文件夹中,所以:

#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); ?>">