laravel how to display flash message with confirm message - php

hi i'm trying to display flash message with confirm(like confirm alert in javascript) . i m trying this below code its does not display flash message. please help me to resolve the problem.
Session::flash('flash_message', '<b>Warning!</b> Are you sure you want to delete your this event?');
Session::flash('flash_type', 'alert-danger');
if($event) {
$event->delete();
return Redirect::to('events')->with('message', 'Event deleted successfully!');
} else {
Session::flash('alert-class', 'alert-danger');
return Redirect::to('events')->with('message', 'Please try again');
}

In your view, where you have the button to delete a record for example you should have something like this:
#if (Session::has('message'))
<div class="alert alert-info">{{ Session::get('message') }}</div>
#endif
{{ Form::open(array('route' => array('events.destroy', $id), 'method' => 'delete')) }}
<button type="submit" href="{{ URL::route('events.destroy', $id) }}" class="btn btn-danger btn-mini" onclick="if(!confirm('Are you sure delete this record?')){return false;};">Delete</button>
{{ Form::close() }}
In your controller:
public function destroy($id)
{
$evento = Evento::find($id);
$evento->delete();
Session::flash('success', 'Event delete successfully!');
return Redirect::to('eventos');
}

Related

Laravel 5.7 session flush message is shown many times, although it should appear only once

Faced a problem) As far as I know, Session::flash(...) should be recorded in a session for only one show, but it does not disappear for me, when I update the page, I always have it.
Here is an example code:
public function update(Request $request){
$this->validate($request, [
'amount' => 'required|integer'
]);
$user = Auth::user();
$user->balance += $request->amount;
$user->save();
Session::flash('success', "Balance updated");
return redirect('/balance');
}
The message is displayed like this
#if(Session::has('success'))
<div class="alert alert-success alert-dismissable">
{{ Session::get('success') }}
</div>
#endif
#if(Session::has('error'))
<div class="alert alert-danger alert-dismissable">
{{ Session::get('error') }}
</div>
#endif
Help, please, I can not understand what the problem is. I will be very grateful.
try this, i hope help you
public function update(Request $request){
$this->validate($request, [
'amount' => 'required|integer'
]);
$user = Auth::user();
$user->balance += $request->amount;
$user->save();
return redirect('/balance')->with('success', 'Balance updated');
}
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
for more info please check document :
https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data
and
https://laravel.com/docs/5.7/session#flash-data

Laravel 5.2 - Delete from db

I am using Laravel Framework version 5.2.45.
I have created a simple view that outputs my todos:
#foreach($todos as $todo)
{{ $todo->todo }} <button href="{{ route('todo.delete', ['id' => $todo->id]) }}" class="btn btn-danger">x</button>
<hr>
#endforeach
Within my routes I have created the following route to delete a todo:
Route::get('/todo/delete/{id}', [
'uses' => 'TodosController#delete',
'as' => 'todo.delete'
]);
Within my TodosController I created the following delete method:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Todo;
class TodosController extends Controller
{
public function delete($id) {
$todo = Todo::find($id);
$todo->delete();
return redirect()->back();
}
// ...
When I press the button in the frontend nothing happens. I do not get any error...
Any suggestions what is wrong with my implementation?
Appreciate your replies!
You are using button not tag
turn your code from
#foreach($todos as $todo)
{{ $todo->todo }} <button href="{{ route('todo.delete', ['id' => $todo->id]) }}" class="btn btn-danger">x</button>
<hr>
#endforeach
to
#foreach($todos as $todo)
{{ $todo->todo }} x
<hr>
#endforeach
Try Below code, You have used a button instead of a tag
#foreach($todos as $todo)
{{ $todo->todo }} x
<hr>
#endforeach
You should do like this :
Delete Button :
<a class="btn btn-primary" href="{{ route('todo.delete',$todo->id) }}">Delete</a>
And delete function look like below :
public function delete($id) {
try {
$delete_flag = Todo::where(['id' => $id])->first();
$delete_flag->delete();
return redirect()->back()->with('success', 'Todo deleted successfully');
} catch (Exception $ex) {
return redirect()->back()->with('error', 'Something went wrong');
}
}
#foreach($todos as $todo)
{{ $todo->todo }} x
#endforeach
delete code--
$toDo = Todo::findOrFail($id)->delete();
if($toDo){
return response()->josn(['message'=>'deleted']);
}

Laravel delete with confirm

I try to delete with confirm like, using a controller method delete:
function delete($id) {
$list = Todolist::find($id);
return view('lists.delete')->with('list',$list);
}
and corresponding delete.blade.php:
{!! Form::open(array('route' => array('lists.destroy', $list->id), 'method' => 'delete', 'class' => 'form')) !!}
<button type="submit" class="btn btn-sucess">Delete</button>
<button type="submit" onClick="history.back()">Cancel</button>
{!! Form::close() !!}
then also a controller destroy-method
function destroy($id) {
$list = new Todolist;
//$list->delete($id);
echo
return view('lists.confirmdelete')\Redirect::route('lists.index')
->with('message', 'Task deleted!');
////how to aply 5 second sleep for showing message 'Task deleted!'???
}
and confirmdelete.blade.php
<h1>{{ $list->name }}</h1>
<p>{{ $list->description }}</p>
<p><b>{{ $message }}</b></p>
How to do, that it display "Task deleted!" messsage e.g 5 seconds and then two steps back to an index action?
In your destroy method you have to do a little tweek & also have to use a little bit of js
Change destroy method this to
function destroy($id) {
$list = new Todolist;
$data = [
'name' => $list->name,
'description' => $list->description,
];
$list->delete($id);
$data['message'] = 'Task deleted!';
$data['redirectRoute'] = route('lists.index');
return view('lists.confirmdelete', $data);
}
and in confirmdelete.blade.php
<h1>{{ $name }}</h1>
<p>{{ $description }}</p>
<p><b>{{ $message }}</b></p>
<script>setTimeout(function(){ window.location.href = '{{ $redirectRoute }}' }, 5000);</script>
Easier with:
<button class="btn btn-link"
onclick="return confirm('Are you sure you want to delete the record {{ $user->id }} ?')">
DELETE
</button>
In this case, you open a popup with a confirmation if you want to delete a record before clicking. Obiously you need to put
$user = User::find($id);
in the delete method if you want an id in the message.
session variable best for given problem
try in controller method
function destroy($id) {
//AFTER SUCCESS YOUR LOGIC
Session::flash('message', 'Status Changed');
Session::flash('alert-class', 'alert-success');
return redirect('/index');
}
in blade file you can check like this demo
#if(Session::has('message'))
<div class="alert {{ Session::get('alert-class') }} fade-in" id="alert">
×
{{ Session::get('message') }}
</div>
#endif
Session flash is best to use here it will poof once it print. and you can use JS for after 5 second fadeout.
<script>
$("#alert").fadeTo(2000, 500).slideUp(500, function(){
$("#alert").slideUp(500);
});
</script>

Laravel Getting Error with Validations

I am trying to Get Validation Errors on index.blade.php having issues:
When I fill both the fields then it goes well if i just put an Echo or Return in getLogin Controller.
When I just fill one field and it works good if i just put and echo or Return but not giving validation errors, with Validation Errors it only shows, "Something went Wrong"
Code for index.blade.php
<section class="mainWrap">
<div class="headingfirst"><img src="{{ URL::asset('css/des.png') }}" width="78"></div>
<div class="sedhead">Hey User!!!! Try to Login</div>
<div class="againtext">Sign In To Your Account</div>
<article class="FormContainer">
#foreach($errors as $error)
<div class="errors">{{ $error }} </div>
#endforeach
<img class="profile-img" src="{{ URL::asset('css/avatar_2x.png')}}">
{{ Form::open(array('class'=> 'SetMe')) }}
{{ Form::text('email',null, array('placeholder'=>'Email','class'=>'insi')) }}
{{ Form::password('password',array('placeholder'=>'Password','class'=>'dnsi')) }}
{{ Form::submit('Sign In', array('class'=>'SignIn')) }}
{{ Form::close() }}
</article>
</section>
Code for AuthController.php
<?php
class AuthController extends Controller{
public function GetLogin() {
return View::make('layouts.index');
}
public function LogInfo() {
$rules = array('email' => 'required','password' =>'required');
$validator = Validator::make(Input::all(),$rules);
if($validator->fails()){
return Redirect::route('login')
->withErrors($validator);
}
else{
}
}
}
Code for Routes.php
Route::get('login', array('uses'=>'AuthController#GetLogin' ));
Route::post('login', array('uses'=>'AuthController#LogInfo'));
even when i put the Auth Code it don't show anything except "Something goes wrong". but while working with just Echos it works properly
In validation failed statement,
You need to use return Redirect::to('login') instead of return Redirect::route('login').
In index.blade.php, it should be like -
#foreach($errors->all() as $error)
<div class="errors">{{ $error }} </div>
#endforeach
instead of
#foreach($errors as $error)
<div class="errors">{{ $error }} </div>
#endforeach
Also here is my suggestion. if you are currently developing an application using laravel, it is the best to enable debug. Open laravel/app/config/app.php and make sure 'debug' => 'true'. It will help you see what is detailed error messages with stack traces.

Authantication returning false Laravel 4

I'm writing a simple authentication form with laravel.
After trying two different tutorials, it's still returning false.
Register is working fine, but my login isn't.
Here is the LoginController:
public function getLogin() {
$this->layout->content = View::make('user.login');
}
public function postSignin() {
if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password'))))
{
return Redirect::to('user/dashboard')->with('message', 'You are now logged in!');
}
else
{
return Redirect::to('user/login')
->with('message', 'Your username/password combination was incorrect')
->withInput();
}
}
public function getDashboard() {
$this->layout->content = View::make('user.dashboard');
}
The route:
Route::controller('user', 'UserController');
and the view:
{{ Form::open(array('url'=>'user/signin', 'class'=>'form-signin')) }}
<h2 class="form-signin-heading">Please Login</h2>
{{ Form::text('username', null, array('class'=>'input-block-level', 'placeholder'=>'Username')) }}
{{ Form::password('password', array('class'=>'input-block-level', 'placeholder'=>'Password')) }}
{{ Form::submit('Login', array('class'=>'btn btn-large btn-primary btn-block'))}}
{{ Form::close() }}
In your form you have an input for username but in your controller you have Input::get('email').
You have to edit your form like that...
{{ Form::text('email', null, array('class'=>'input-block-level', 'placeholder'=>'Username')) }}
...or your controller like that
Auth::attempt(array('email'=>Input::get('username'), 'password'=>Input::get('password')))
I found out that I hadn't set a key with
php artisan key:generate
and my password field was only 32 characters long instead of 60.
Finally got it to work!

Categories