在我的网站上,注册用户可以通过上传图片和撰写说明来创建帖子。我根据用户的图像名称和上传图像的时间创建了一个独特的图像名称。创建名称后,它将存储在post数据库中。成功存储图像名称,实际图像也存储。然而,当涉及到在帖子上实际显示图像时,没有任何东西出现,因为它无法找到图像。我似乎无法弄清楚为什么会这样。
这是我的PostController.php类:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function getDashboard(){
$posts = Post::orderBy('created_at', 'desc')->get();
return view('dashboard', ['posts' => $posts]);
}
public function postCreatePost(Request $request){
$this->validate($request, [
'body' => 'required',
'cover_image' => 'image|nullable|max:1999'
]);
if($request->hasFile('cover_image')){
$filenameWithExt = $request->file('cover_image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('cover_image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
$path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
} else{
$fileNameToStore = 'noimage.jpg';
}
$post = new Post();
$post->body = $request['body'];
$post->cover_image = $fileNameToStore;
$message = 'There was an error';
if($request->user()->posts($post)->save($post)){; //points here
$message = 'post successfully created';
}
return redirect()->route('dashboard')->with(['message' => $message]);
}
public function getDeletePost($post_id){
$post = Post::where('id', $post_id)->firstOrFail();
$post->delete();
return redirect()->route('dashboard')->with(['message' => 'post deleted']);
}
}
以下是我的观点:
<section class="row new-post">
<div class="col-md-6 col-md-offset-3">
<form action="{{ route('postcreate') }}" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="cover_image" class="form-control" id="cover_image">
</div>
<div class="form-group">
<textarea class="form-control" name="body" rows="5" placeholder="your post"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create post</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
</section>
@foreach($posts as $post)
<section class="row posts">
<div class="col-md-6">
<article class="post">
<p>{{ $post->body }}</p>
<div class="info">Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</div>
<div class="interaction">
<a href="#" class="post-item">Like</a>|
@if(Auth::user() == $post->user)
<a href="#" class="post-item">Edit</a>|
<a href="{{ route('post.delete', ['post_id' => $post->id]) }}" class="post-item">Delete</a>
@endif
</div>
</article>
</div>
<div class="col-md-6">
<img src="/storage/cover_images/{{ $post->cover_image }}">
</div>
</section>
@endforeach
这是我的迁移:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('cover_image');
$table->text('body');
$table->integer('user_id');
});
答案 0 :(得分:3)
您存储为public/cover_images
,但会回显storage/cover_images
。
根据您的回复更新;
为什么不尝试使用
<img src="{{ asset('storage/cover_images/' . $post->cover_image) }}">