我有一个很奇怪的问题。创建我的帖子时,我的Flash消息不起作用,但是当我删除或更新它时,它就起作用了,当我更改重定向方向时,它也起作用。
因此,在store()
方法中,当我将创建的帖子保存到数据库时,然后重定向到/posts
,如果我将其从/posts
更改为{{1} }。
我的/anyotherroute
与update()
相同,它也重定向到store()
,但是在更新的情况下会出现Flash消息,与/posts
相同。
有人知道为什么吗?
所以我只是注意到在本地主机上出现了Flash消息,但是在共享主机上却没有。
Store()
destroy()
update()
public function store(Request $request){
$this->validate($request, [
'title' => 'required',
'body' => 'required',
'cover_image' => 'image|nullable|max:1999'
]);
// Handle file upload
if($request->hasFile('cover_image')){
// Get filename with the extension
$fileNameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $fileName . '_' . time() . '.' . $extension;
// Upload Image
// $path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
$file = $request->file('cover_image');
Storage::disk('uploads')->put('posts_images/' . $fileNameToStore, File::get($file));
}else{
$fileNameToStore = 'noimage.jpg';
}
// create post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->user_id = auth()->user()->id;
$post->cover_image = $fileNameToStore;
$post->save();
return redirect('/posts')->with('success', 'Post Created');
messages()
public function update(Request $request, $id){
$this->validate($request, [
'title' => 'required',
'body' => 'required',
'cover_image' => 'image|nullable|max:1999'
]);
// Handle file upload
if($request->hasFile('cover_image')){
// Get filename with the extension
$fileNameWithExt = $request->file('cover_image')->getClientOriginalName();
// Get just filename
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('cover_image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $fileName . '_' . time() . '.' . $extension;
// Upload Image
// $path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);
$file = $request->file('cover_image');
Storage::disk('uploads')->put('posts_images/' . $fileNameToStore, File::get($file));
}
// create post
$post = Post::find($id);
$post->title = $request->input('title');
$post->body = $request->input('body');
if($request->hasFile('cover_image')){
$post->cover_image = $fileNameToStore;
}
$post->save();
return redirect('/posts')->with('success', 'Post Updated');
posts / index()
@if(count($errors) > 0)
@foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
@endforeach
@endif
@if(session('success'))
<div class="alert alert-success">
{{session('success')}}
</div>
@endif
@if(session('error'))
<div class="alert alert-danger">
{{session('error')}}
</div>
@endif`
app_layout()
@extends('layouts/app')
@section('content')
<h1>Posts</h1>
@if(count($posts) > 0)
@foreach($posts as $post)
<div class="well">
<div class="row">
<div class="col-md-4 col-sm-4">
{{-- <img src="/storage/cover_images/{{$post->cover_image}}" alt="error" style="width: 100%;"> --}}
<img src="{{url('/uploads')}}/posts_images/{{$post->cover_image}}" style="width: 100%; max-height: 200px;">
</div>
<div class="col-md-8 col-sm-8">
<h3><a href="{{url('/posts')}}/{{$post->id}}">{{$post->title}}</a></h3>
<small>Written on {{$post->created_at}} by {{@$post->user->name}}</small>
</div>
</div>
</div>
@endforeach
<div id="pagination">
{{$posts->links()}}
</div>
@else
<p>No posts found.</p>
@endif
@endsection()
答案 0 :(得分:0)
因此由于某种原因,当我登录到我的Google帐户时,它没有出现在Chrome中。我去了我的笔记本电脑,在那里也使用了Chrome,并且在笔记本电脑上可以正常使用,然后我在计算机上更改了google帐户,它也可以正常使用。当我在我的google帐户上,甚至清除了历史记录,缓存和cookie时,它都无法正常工作,但是我想我不介意它是否对其他人有用。 无论如何,那确实是愚蠢的问题,很抱歉让您无所事事:(