如何使用laravel存储和检索图像?我在sql server中有一个“image”数据类型的图像字段。我想保存图像并使用laravel检索它?在下面找到我使用的代码。
$img_path = Input::file('file') -> getRealPath();
$image_data = file_get_contents($img_path);
$base64 = base64_encode($image_data);
$logoTypes = new logoTypes;
$logoTypes -> Type = "Image";
$logoTypes -> image = $base64;
$logoTypes -> created_by = Auth::user()->id;
$logoTypes -> updated_by = Auth::user()->id;
$logoTypes -> save();
但是,我收到以下错误,
{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[22018]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Operand type clash: nvarchar(max) is incompatible with image (SQL: insert into [logoTypes] ([Type], [image], [created_by], [updated_by], [updated_at], [created_at]) values (Image, \/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wCEAAkGBxQSEhUUEhISFBQUFhQUFBQVFRQUFRQUFxQXFhQUFxQYHCggGBolHBQUITIhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGxAQGywkICQsLCwvLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLPMnEDzVTr3qpqgbmkfiA5qp2ILNOvOqqdeocA3NMcRXDiSyjsQ6ql2I9UNkV3Nc7FFwYwFjH4l1Q78S6qu6J3DcPxwDihax...., 1, 1, 2014-06-23 17:20:18.000, 2014-06-23 17:20:18.000))","file":"D:\\wamp\\www\\Lhrms\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php","line":555}}
答案 0 :(得分:0)
只需使用Laravel Staper:https://github.com/CodeSleeve/laravel-stapler
Laravel-Stapler是一个基于Stapler的Laravel框架文件上传包。它可用于将文件文件上载(作为附件对象)添加到ORM记录中。