【发布时间】:2021-03-16 12:42:23
【问题描述】:
我正在尝试将数据(标题、描述和调整大小的图像)传递到我的数据库
这是存储功能,现在它将我的图像保存在该路径中,但不会将它们保存在数据库中
如果我删除调整大小的图像,商店功能可以工作。
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:10240',
'description' => 'required',
]);
$path = $request->file('image');
$filename = $path->getClientOriginalName();
$image_resize = Image::make($path->getRealPath());
$image_resize->resize(800, 800);
$image_resize->save(public_path('images/' .$filename));
$post = new Post;
$post->title = $request->title;
$post->description = $request->description;
$post->image = $image_resize;
$post->save();
return redirect()->route('posts.index')
->with('success','Post has been created successfully.');
}
enter image description here enter image description here
这是新的商店功能
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:10240',
'description' => 'required',
]);
$path = $request->file('image');
$filename = $path->getClientOriginalName();
$image_resize = Image::make($path->getRealPath());
$image_resize->resize(800, 800);
$image_resize->save(public_path('images/' .$filename));
$post = new Post;
$post->title = $request->title;
$post->description = $request->description;
$post->image = 'images/' .$filename;
$post->save();
return redirect()->route('posts.index')
->with('success','Post has been created successfully.');
}
这是刀片代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel 8 CRUD Tutorial</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" >
</head>
<body>
<div class="container mt-2">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 Post CRUD Tutorial</h2>
</div>
<div class="pull-right mb-2">
<a class="btn btn-success" href="{{ route('posts.create') }}"> Create New Post</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>S.No</th>
<th>Image</th>
<th>Title</th>
<th>Description</th>
<th width="280px">Action</th>
</tr>
@foreach ($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td><img src="{{ Storage::url($post->image) }}" height="75" width="75" alt="" /></td>
<td>{{ $post->title }}</td>
<td>{{ $post->description }}</td>
<td>
<form action="{{ route('posts.destroy',$post->id) }}" method="POST">
<a class="btn btn-primary" href="{{ route('posts.edit',$post->id) }}">Edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
{!! $posts->links() !!}
</body>
</html>
【问题讨论】:
-
数据库中的“图像”列是哪种类型?
标签: php laravel image store image-resizing