Session::flash('success', 'Data has been saved successfully'); - laravel

I am using the following code to show flash message, but it is not working.
PostController
public function store(Request $request){
$this->validate($request,array(
'title'=>'required|max:255',
'slug' =>'required|alpha_dash|min:5|max:255|unique:posts,slug',
'body' =>'required'
));
//store in the database
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->body = $request->body;
$post->save();
//This code will generate flash message about success or failure about data insert
Session::flash('success', 'Data has been saved successfully!');
//Redirect to another page
return redirect()->route('posts.show', $post->id);
}
Then to display it the following code is used:
message.blade.php
#if(Session::has('success'))
<div class="alert alert-success" role= "alert">
<strong>Successful:</strong>
{{ Session::get('success') }}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong>Errors:</strong>
<ul>
#foreach($errors as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
The code above is not showing any flash message. But when
Session::flash('success', 'Data has been saved successfully!');
is written as:
Session::put('success', 'Data has been saved successfully!');
the flash message is displayed and does not disappear.
The routes.php is :
Route::group(['middleware' => ['web']], function(){
Route::get('auth/login', ['as' =>'login', 'uses'=>
'Auth\AuthController#getLogin']);
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', ['as' => 'logout', 'uses' =>
'Auth\AuthController#getLogout']);
Route::get('auth/register','Auth\AuthController#getRegister');
Route::post('auth/register','Auth\AuthController#postRegister');
Route::get('password/reset/{token?}',
'Auth\PasswordController#showResetForm');
Route::post('password/email',
'Auth\PasswordController#sendResetLinkEmail');
Route::post('password/reset','Auth\PasswordController#reset');
Route::get('contact', 'PagesController#getContact');
Route::get('about', 'PagesController#getAbout');
Route::get('/', 'PagesController#getIndex');
Route::get('reader/{slug}', ['as' => 'reader.single', 'uses' =>
'ReaderController#getSingle'])
->where('slug', '[\w\d\-\_]+');
Route::get('reader', ['as' => 'reader.index', 'uses' =>
'ReaderController#getIndex' ]);
Route::resource('posts', 'PostController');
});
Help please!

1.- Make sure if u are including the message template on your view, in this case your SHOW view
2.- Try replacing your message template for this:
#if(Session::has('success'))
<div class="alert alert-success" role= "alert">
<strong>Successful:</strong>
{!! session('success') !!}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong>Errors:</strong>
<ul>
#foreach($errors as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
Look, i'm using {!! session('success') !!} instead of yours

Related

Email Sending and Validation Failing in Laravel 5.4

I am creating a contact form using bootstrap3 build on Laravel 5.4. When I click the submit button, I expect an email to be sent to my inbox or if there are errors they should be validated on the back end and errors displayed at the top of the form. I am using Laravel collective to build the form, when I fill the form and click the submit button, the page only reloads and no validation happens or in case of correct input no email is sent. Please assist?
Form Section
<div class="col-sm-4 wow animated fadeInLeft">
<div id="success" class="col-sm-12">
#if(Session::has('success'))
<span class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</span>
#endif
#if(count($errors) > 0)
<span class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</span>
#endif
</div>
{!! Form::open(array('route' => 'index.post', 'method' => 'POST','class' => 'contact-form')) !!}
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => ''))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => ''))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Routes File
Route::post('/', 'PagesController#postIndex') ->name('index.post');
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
PagesController
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#pwebk.com');
});
Session::flash('success', 'Hello '.$name.', Your Form was successfully sent');
return redirect()->route('pages.index');
}
public function getIndex(){
return view('pages.welcome');
}

laravel5.2 errors not show up in blade

I am trying solve this wired problem for hours, but still the errors message won't show up in template.
I have make sure the web middleware (which contains StartSession and ShareErrorsFromSession) is wrapped all my route.
Route::group(['namespace'=>'admin','prefix'=>'admin','middleware'=>['web']],function (){
Route::get('index','AdminController#index');
Route::get('addCase','CaseController#create');
Route::any('upload','AdminController#upload');
Route::resource('case', 'CaseController');
});
public function store(Request $resquest)
{
//
$validator = Validator::make($resquest->all(), [
'title' => 'required',
]);
if($validator->passes()){
}else{
return back()->withErrors($validator);
}
}
<form method="post" class="form-horizontal" action="{{action('admin\CaseController#store')}}">
{{ csrf_field() }}
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<button>submit</button>
</form>
please help!
the validation is worked, but the errors vanished
Please try this
In controller
$validator = Validator::make($request->all(),[
'title' => 'required'});
if($validator->fails()) {
return Redirect::back()->withErrors($validator);
}
In view page
#if ($errors->any())
<ul class="alert alert-danger">
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif

laravel 5.2 Restfull Api with admin panel stop working suddenly

I developed admin panel to add,edit,delete users in my website with Restfull Api. edit and delete work fine but add not.
when I add user it may work or this error may appear
The localhost page isn’t working
localhost is currently unable to handle this request.
This is my routes
Route::resource('admin/users','AdminUser');
This is store function in Admin user resource
public function store(Request $request)
{
//rules
$rules = array(
'name' => 'required',
'email' => 'required|email',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required|min:6'
);
/*
validate the data user
**/
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return view('admin.User.create_user')
->withErrors($validator)
->withInput(['page' => 'home']);
}
/*
Store the data user in the database
**/
$user = new User;
$user->name =$request->input('name');
$user->email = $request->input('email');
$user->password=bcrypt($request->input('password'));
$user->role='user';
$user->save(); //error here
//redirect
return redirect('admin/users')->with('message', 'Successfully added user!');
}
And this is create_user.blade.php
#extends('layouts.layout')
#section('content')
<section id="advertisement">
<div class="container">
<img src="{{asset('images/shop/advertisement.jpg')}}" alt="" />
</div>
</section>
<section>
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="left-sidebar">
#include('shared.sidebaradmin')
</div>
</div>
<div class="features_items"><!--features_items-->
<h2 class="title text-center">Add New Product</h2>
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('url' => 'admin/users')) }}
<div class="form-group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name',null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::email('email',null, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password_confirmation', 'Confirm Password') }}
{{ Form::password('password_confirmation', array('class' => 'form-control')) }}
</div>
{{ Form::submit('Add a new user !', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div><!--features_items-->
</div>
</div>
</section>
#endsection

Laravel 5.2 TokenMismatchException

I'd like to write a code in Laravel 5.2 which would upload photos, my problem is that once I start uploading many photos at once the site goes down.
It gives me this error: TokenMismatchException in VerifyCsrfToken.php line 67:
This error should appear only when the {{ csrf_field() }}, is missing from the form, but in this case it isn't, it's right there.
It works perfectly with less images. What could be the problem?
Controller
public function store(Request $request)
{
$rules = array(
'picturess' => 'mimes:jpeg,jpg,bmp,png',
);
$messages = array(
'mimes' => 'A feltöltetni kívánt kép nem felel meg a kritériumoknak. (Ilyen lehet a kép kiterjesztése: jpeg, jpg, bmp vagy png. A kép se lehet bármekkora.)',
'integer' => 'A beírt szőveg nem szám.',
'required' => 'Ennek a mezőnek a kitőltése kötelező.',
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::back()->withInput()->withErrors($validator);
}
if (Input::hasFile('pictures')) {
$files = $request::file('pictures');
$file_count = count($files);
$uploadcount = 0;
$destinationPath = 'uploads';
$userId = Auth::user()->id;
foreach ($files as $file) {
if ($file->isValid()) {
$extension = $file->getClientOriginalExtension();
$pictureFileName = $this->makePictureFileName(0, $extension);
$thumbnailPictureFileName = $this->makePictureFileName(1, $extension);
Log::info('pictureFileName: '.$pictureFileName);
if ($file->move($destinationPath, $pictureFileName)) {
$uploadcount++;
$img = Image::make($destinationPath . '/' . $pictureFileName);
//$img = Image::make($file->getClientOriginalName());
$img->resize(277, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save($destinationPath . '/' . $thumbnailPictureFileName);
$picture = new Picture;
$picture->filename = $pictureFileName;
$picture->thumbnail_filename = $thumbnailPictureFileName;
$picture->user_id = $userId;
$picture->save();
}
} else {
Session::flash('picture-error', 'A feltöltetni kívánt kép nem megfelelő. (Valószínűleg túl nagy.)');
return redirect()->back()->withInput()->withErrors($validator);
}
}
if ($uploadcount == $file_count) {
Session::flash('success', 'A képek feltöltése sikeres.');
} else {
return redirect()->back()->withInput()->withErrors($validator);
}
}
//return Redirect::to('pictures/create');
return redirect()->back()->withInput()->withErrors($validator);
}
View
#extends('layouts.site')
#section('content')
<div class="row">
<div class="col-md-12">
{!! Form::open(array('method' =>'POST', 'url' => 'pictures', 'class' => 'uk-form', 'files'=> true)) !!}
{{ csrf_field() }}
<div class="form-group">
{!! Form::label('pictures', 'Kép', array('class' => '')) !!}
<div class="uk-form-controls">
{!! Form::file('pictures[]', array('class' => '','multiple'=>true)) !!}
</div>
</div>
<div class="form-group">
<button class="uk-button">Küldés</button>
</div>
#if ($errors->has('success'))
<div class="alert alert-danger" role="alert">
<p>{{ $errors->first('success') }}</p>
</div>
#endif
#if(Session::has('picture-error'))
<div class="alert alert-danger" role="alert">
<p class="errors">{!! Session::get('error') !!}</p>
</div>
#endif
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{!! Form::close() !!}
</div>
</div>
#stop
Route
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'SiteController#index');
Route::get('admin', 'AdminController#index');
Route::resource('pictures', 'PicturesController');
Route::resource('users', 'UsersController');
Route::auth();
});
{!! Form::open... will automatically add CSRF field protection, so you don't need this {{ csrf_field() }}.

MethodNotAllowedHttpException Laravel

I'm having trouble with an error that I'm getting in laravel. When I run my code on localhost I don't have any issues, but when I placed laravel in demo live server which is server ('https') I get the MethodNotAllowedHttpException error.
Here's my code for my route
Route::post('post_reminder', function(){
$creds = array(
'email' => Input::get('email')
);
$rules = array(
'email' => 'required|email'
);
$messages = array(
'email' => 'The :attribute needs to be an real email'
);
$validator = Validator::make($creds, $rules,$messages);
if($validator->fails())
{
return Redirect::route('getReminder')->withErrors($validator);
}
else
{
return Password::remind($creds);
}
});
And here's the form code
<div id="reset_container">
{{ Form::open(array('url' => 'post_reminder')) }}
<h1 id="pass_recovery_text">Password Recovery Form</h1>
<p>
#if (Session::has('error'))
<li id="error">{{ trans(Session::get('reason')) }}</li>
#elseif (Session::has('success'))
<li id="error">An e-mail with the password reset has beensent.</li>
#endif
#foreach($errors->all() as $error)
<li id="error">{{ $error }}</li>
#endforeach
{{ Form::label('email', 'Please enter you email: ') }}
{{ Form::text('email','',array('id' => 'forgot')) }}
{{ Form::submit('Reset') }}<br /><br /><br />
{{ HTML::link('/', 'Have a account Sign-In', array('id' => 'sign-in')) }}
</p>
{{ Form::close() }}
</div>
<div id="reset_container">
{{ Form::open(array('url' => 'post_reminder','method' => 'post')) }}
<h1 id="pass_recovery_text">Password Recovery Form</h1>
<p>
#if (Session::has('error'))
<li id="error">{{ trans(Session::get('reason')) }}</li>
#elseif (Session::has('success'))
<li id="error">An e-mail with the password reset has beensent.</li>
#endif
#foreach($errors->all() as $error)
<li id="error">{{ $error }}</li> #endforeach
{{ Form::label('email', 'Please enter you email: ') }}
{{ Form::text('email','',array('id' => 'forgot')) }}
{{ Form::submit('Reset') }}
{{ HTML::link('/', 'Have a account Sign-In', array('id' => 'sign-in')) }} </p>
{{ Form::close() }}
Have you tried force the route to be served over https?
Route::post('post_reminder', array('https', function(){
$creds = array(
'email' => Input::get('email')
);
$rules = array(
'email' => 'required|email'
);
$messages = array(
'email' => 'The :attribute needs to be an real email'
);
$validator = Validator::make($creds, $rules,$messages);
if($validator->fails())
{
return Redirect::route('getReminder')->withErrors($validator);
}
else
{
return Password::remind($creds);
}
}));
Referencing to http://laravel.com/docs/4.2/routing here.

Resources