Laravel delete with confirm - php

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>

Related

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 how to display flash message with confirm message

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

viewing my database in the view at laravel

sorry if this is a very newbie Q..
but please help me to solve this problem. plus give me the reason about why this error happened..
this is my edit view
new.blade.php
#section('content')
#include('common.show_error')
{{Form::open(array('url'=>'author/update', 'method'=>'PUT'))}}
<p>
{{ Form::label('name', 'Name: ') }}</br>
{{ Form::text('name', $author->name) }}
</p>
<p>
{{ Form::label('bio', 'Biography: ') }}</br>
{{ Form::textarea('bio', $author->bio) }}
</p>
{{ Form::hidden('id', $author->id) }}
<p>{{ Form::submit('Edit Data') }}</p>
#stop
this is my show view
show.blade.php
#extends('layouts.default')
#section('content')
<h1>{{ $author->name }}</h1>
<p>{{ $author->bio }}</p>
<p>{{ $author->updated_at }}</p>
<span>
{{ HTML::linkRoute('authors', 'Home') }} |
{{ HTML::linkRoute('edit_author', 'Edit', array($author->id)) }} |
{{ Form::open(array('url'=>'author/destroy', 'method'=>'DELETE', 'style'=>'display: inline;')) }}
{{ Form::hidden('id', $author->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
</span>
#stop
this is my controller
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
Member::where('id','=',$id)->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated');
}
}
the case: when I try to edit data using edit button. it said:
"Trying to get property of non-object laravel"
and when I check at the error log. it refers to
<h1>{{ $author->name }}</h1>
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
$author = Member::find($id);
$author->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated')
->with('author', $author);
}
}
Little changes in your controller, try it :) In your code, you are not send variable "author" into your view.

Laravel 4 Form Submission and Input Retrieval

I am a beginner in Laravel. I am trying to make a simple login form, by using a controller to manipulate the input. However everytime the code just ignore the controller function and keep calling the index everytime I submit. Please advise.
Here is the code for my form
{{ Form::open(array('action' => 'CoverController#authent')) }}
<div class="col-md-3 text-box pull-left">
{{ Form::email('email', '', array('placeholder'=>'Email')); }}
</div>
<div class="col-md-3 text-box pull-left">
{{ Form::password('password', array('placeholder'=>'Password')); }}
</div>
<div class="clearfix"> </div>
<div class="con-button">
{{ Form::submit('Sign Up / Log In'); }}
</div>
{{ Form::close() }}
Below is my routes
Route::get('/',array('as'=>'users','uses'=>'CoverController#index'));
Route::post('/','CoverController#authent');
Here is my controller function
class CoverController extends BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$view = View::make('cover');
return $view;
}
public function authent()
{
$email = Input::get('email');
$pwd = Input::get('password');
$view = View::make('formoid')->with('email',$email)->with('password',$pwd);
return $view;
}
}
With the above code,everytime the login button is pressed, the index() function is called instead of authent(), what am I doing wrong?
Try:
{{ Form::open(array('url' => '/', 'method' => 'post')) }}
Form Doc

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