I am unable to find any article on how to store and retrieve image in postgres using bytea in laravel project. I am particularly looking on save and and retrieve data using ajax call.
Asked
Active
Viewed 904 times
-1
-
Click [here](https://stackoverflow.com/questions/22288898/insert-an-image-in-postgresql-database/46314519?noredirect=1#comment93152073_46314519)! Here we have given three kinds of solutions try this one once.And let me know if you still have any issues. We can save the image as blob ,bytea . – Ravi Jan 24 '20 at 11:03
1 Answers
-3
public function store(Request $request)
{
$this->validate($request,[
'title' =>'required',
'image' => 'mimes:jpeg,jpg,bmp,png',
]);
$image = $request->file('image');
$slug = str_slug($request->title);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1600,1066);
if (!file_exists('storage/uploads/post'))
{
mkdir('storage/uploads/post',0777,true);
}
//$image->move('storage/uploads/post',$imagename);
$image_resize->save('storage/uploads/post/'.$imagename);
}else{
$imagename = "default.png";
}
$post = new Post();
$post->user_id = Auth::id();
$post->title = $request->title;
$post->slug = str_slug($request->title);
$post->image = $imagename;
$post->save();
Toastr::success('Post Successfully Save:)','Success');
return redirect()->route('admin.post.index');
}
// view page return
public function index()
{
$posts = Post::latest()->get();
return view('admin.post.index',compact('posts'));
}
// View
@foreach($posts as $post)
<img src="{{asset('storage/uploads/post/'.$post->image)}}" alt="{{$post->title}}" />
@endforeach

Md Azizur Rahman
- 359
- 3
- 7
-
he is looking for an example of code to store the file to postgresql database, not to a file. – Jacky Supit Jul 14 '22 at 07:49