0

I am using laravel 5.4. I am facing a big problem from last few days in my app. Any error or success message is not showing properly. When I created those feature, those worked fine. All message was showing properly. But from last few days I am facing this issue. When I try to add some content, if validation fail then error message is not shows sometime and shows sometime. Same when validation is success. I can see those data stored in the database properly but success message is not shows all the time. Sometime shows.
Here is my from to create a notice

 {!! Form::model($single_notice,['route'=>['notice.update',$single_notice->id],'method'=>'PUT','files'=>true]) !!}
  <div class="col-lg-7">
    <div style="display: block;" id="rulesformitem" class="formitem">
      {{ Form::textarea('notice', null,array('class'=>'form-control notice-textarea')) }}
    </div>
  </div>
@if($single_notice->general == 'general')
  <div class="col-lg-3 text-center" style="margin-top: 15px">
     <label for="rules" id="ruleslabel">Notice For:</label>
     <br>
     <input onclick="document.getElementById('custom').disabled = true; document.getElementById('charstype').disabled = false;" type="radio" name="type" value="general" checked="checked">General
     <br>
      <input onclick="document.getElementById('custom').disabled = false; document.getElementById('charstype').disabled = true;" type="radio" name="type"  >Demo/Final
        <br>
        <label for="rules" id="ruleslabel">Date & Time: <br> 01-01-2017 10(hr):30(m) <small>(This format)</small> </label>
        <br>
        <input type="datetime-local" class="form-control" name="demoOrfinal" id="custom" >
        <br>

  </div>  
  @else
  <div class="col-lg-3 text-center" style="margin-top: 15px">
     <label for="rules" id="ruleslabel">Notice For:</label>
     <br>
     <input onclick="document.getElementById('custom').disabled = true; document.getElementById('charstype').disabled = false;" type="radio" name="type" value="general">General
     <br>
      <input onclick="document.getElementById('custom').disabled = false; document.getElementById('charstype').disabled = true;" type="radio" name="type"  checked="checked">Demo/Final
        <br>
        <label for="rules" id="ruleslabel">Date & Time: <br>
  {{ Carbon\Carbon::parse($single_notice->demoOrfinal)->format('H:i d-m-Y') }}
        </label>
        <br>
        <input type="datetime-local" class="form-control" name="demoOrfinal" id="custom" >
        <br>

  </div>
  @endif
  <div class="col-lg-2 text-center" style="margin-top: 20px">
    <label>Upload File:</label>

    @if($single_notice->image_link != 'null')
    <input type="button" class="form-control" id="loadFileXml" value="Image" style="margin-bottom: 5px" onclick="document.getElementById('image').click();" />
          {{ substr(strip_tags($single_notice->image_link),0,5) }}
          {{ strlen(strip_tags($single_notice->image_link)) > 5 ? "...jpg" : "" }}
    @endif
    <input type="file" style="display:none;" id="image" name="notice_image"/>
    <input type="button" class="form-control" id="loadFileXml" value="PDF" onclick="document.getElementById('pdf').click();" />
    @if($single_notice->pdf_link != 'null')
    <small>{{ substr(strip_tags($single_notice->pdf_link),0,5) }}
          {{ strlen(strip_tags($single_notice->pdf_link)) > 5 ? "...pdf" : "" }}
    </small>

    @endif
    <input type="file" style="display:none;" id="pdf" name="notice_pdf"/>
    <button class="form-control btn-info" style="margin-top: 5px;">Update Notice</button>

  {!! Form::close() !!}

Here is method to create a notice

public function store(Request $request)
{
    //dd($request);
    $teacher_id = Auth::user()->id;
    $this->validate($request,array(
        'notice'       => 'required|min:5',
        'notice_image' => 'sometimes|mimes:jpeg,jpg,png',
        'notice_pdf'   => 'sometimes|mimes:pdf'
    ));
    if($request->type == 'general')
    {
        $this->validate($request,array(
            'type' => 'required',
        ));   
    }
    if($request->type == 'on') {
        $this->validate($request,array(
        'demoOrfinal' => 'required|date',
        ));
    }
    $add_notice = new Notice;
    $add_notice->notice      = $request->notice;
    $add_notice->general     = $request->type;
    $add_notice->demoOrfinal = $request->demoOrfinal;
    $add_notice->posted_by  = $teacher_id;
    $add_notice->image_link  = $request->notice_image;
    $add_notice->pdf_link    = $request->notice_pdf;

    if ($request->hasFile('notice_image')) {
        $file = $request->file('notice_image');
        $filename = time().'.'.$file->getClientOriginalName();
        $location = public_path('file/'.$filename);
        Storage::put($filename,file_get_contents($file));
        $add_notice->image_link = $filename;

    }

    if ($request->hasFile('notice_pdf')) {
        $file = $request->file('notice_pdf');
        $filename = time().'.'.$file->getClientOriginalName();
        $location = public_path('file/'.$filename);
        Storage::put($filename,file_get_contents($file));
        $add_notice->pdf_link = $filename;

    }
    $add_notice->save();
    Session::flash('success','Notice Has Been Posted. Thanks :-)');
    return redirect()->back();
}

Here Is my error message showing code

 @if(Session::has('success'))
 <div class="alert alert-success">
 <button type="button" class="close" data-dismiss="alert" aria- 
 hidden="true">&times;</button>
 <strong>{{ Session::get('success') }}</strong>
 </div>
  @endif
 @if(Session::has('denger-success'))
  <div class="alert alert-danger">
 <button type="button" class="close" data-dismiss="alert" aria- 
  hidden="true">&times;</button>
 <strong>{{ Session::get('denger-success') }}</strong>
 </div>
 @endif
 @if(count($errors))
 <div class="alert alert-danger validation-error-message">
 <button type="button" class="close" data-dismiss="alert" aria- 
 hidden="true">&times;</button>
<strong>Error:</strong>
<ul>
  @foreach($errors->all() as $error)
    <li>
      {{ $error }}
    </li>
  @endforeach
 </ul> 
 </div>
 @endif

Sorry for my poor english. Thanks in advance.

6
  • please post code of one of the methods with validation & success/error messages. Commented Apr 14, 2018 at 17:17
  • 1
    See How to create a Minimal, Complete, and Verifiable example. We cannot help you without code. Commented Apr 14, 2018 at 17:18
  • added code. Please see Commented Apr 14, 2018 at 17:24
  • Have you moved sessions from file to database? Commented Apr 14, 2018 at 17:29
  • yes @DimitriMostrey Commented Apr 14, 2018 at 17:50

1 Answer 1

1

When you run php artisan session:table and php artisan migrate, change the .env file to SESSION_DRIVER=database, the ´sessions´ table is created and put into use.

The schema of sessions is as follows:

Schema::create('sessions', function (Blueprint $table) {
        $table->string('id')->unique();
        $table->text('payload');
        $table->integer('user_id')->nullable();
        $table->string('ip_address')->nullable();
        $table->text('user_agent');
        $table->integer('last_activity');
    });

It happens that the text for payload is too long and the session gets lost. If you 'upgrade' to mediumtext you fix the issue.

You can either do that in the database or create a new migration php artisan make:migration change_payload_in_sessions_table and add:

Schema::table('sessions', function (Blueprint $table) {
        $table->mediumText('payload')->change();
    });

Of course you need to run php artisan migrate

Sign up to request clarification or add additional context in comments.

4 Comments

still it is doing same problem :-(
I am taking your answer as accepted because You gave me way to solve. I have changed session from database to file again and now it is solved. Your solution should work for others . So I have marked this as solved. Thank you soo much for helping me.
As you are probably working in a test environment, in the config/session.php file, change 'lottery' => [2, 100], to 'lottery' => [0, 100],. That could be another reason why it sometimes doesn't works.
You could also use return redirect()->back()->with('success','...'); or ->with(['success' => '...']) instead of Request::flash(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.