Search code examples
phplaravelhttp-redirectflash-message

Laravel flash message not working in specific method on specific redirect route


I have pretty strange problem. My flash message isn't working when I create post, but works when I delete or update it, it also works when I change redirect direction.

So in store() method when I save created post to DB I then redirect to /posts, redirect happens but no flash message, if I change it from /posts to /anyotherroute it works.

My update() is identical to store() and it also redirects to /posts but flash message appears in case of update, same with destroy().

Anyone knows why is that ?

So i just noticed that on localhost flash message appears, but on shared hosting it doesn't.

Store()

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');

update()

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');

messages()

    @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`

posts/index()

    @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()

app_layout()

 <!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
    <div id="app">

        @include('inc/navbar')
        <div class="container">
          @include('inc/messages')
          @yield('content')
        </div>
    </div>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}"></script>
    <script type="text/javascript" src="{{URL::asset('js/custom.js') }}"></script>
    <script src="/vendor/unisharp/laravel-ckeditor/ckeditor.js"></script>
    <script>
      CKEDITOR.replace('article-ckeditor');
    </script>
</body>
</html>

Solution

  • So for some reason its not appearing in Chrome when im logged into my google account. I went to my laptop and used Chrome there as well and on laptop it worked, then i changed google account on my computer and it worked as well. Its only not working when im on my google account, even tho' i cleared history, cache and cookies, but i guess i don't mind if it works for others. Anyways that was really stupid problem, sorry for bothering you for nothing :(