I am using this delete button to delete task in table
<button class="btn btn-danger delete pull-right"
data-action="/projects/{{ $project->id }}/tasks/{{ $task->id }}"
data-token="{{csrf_token()}}">
<i class="fa fa-trash-o"></i>Delete
</button>
this is delete controller
public function deleteOneProjectTask($projectId, $taskId)
{
DB::table('tasks')
->where('project_id', $projectId)
->where('id', $taskId)
->delete();
return redirect()->route('projects.show')->with('info', 'Task deleted successfully');
}
and routes
Route::delete('projects/{projects}/tasks/{tasks}', [
'uses' => '\App\Http\Controllers\ProjectTasksController#deleteOneProjectTask',
]);
but when I click delete button it is not delete task. but when I refresh it it will delete. how can fix this?
Related
I have a view that show some data and have edit feature, but when clicked edit button, user will be redirected to http://localhost:8000/posts/2/edit, I dont want post id appear in the URL, what should I do?
<table class="table table-bordered">
<tr>
<th width="20px" class="text-center">No</th>
<th>Title</th>
<th>Content</th>
<th width="280px" class="text-center">Action</th>
</tr>
#foreach ($posts as $post)
<tr>
<td class="text-center">{{ ++$i }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->content }}</td>
<td class="text-center">
<form action="{{ route('posts.destroy',$post->id) }}" method="POST">
<a class="btn btn-info btn-sm" href="{{ route('posts.show',$post->id) }}">Show</a>
<a class="btn btn-primary btn-sm" href="{{ route('posts.edit',$post->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Delete?')">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
here is the controller
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
here is the route
Route::get('/', function () {
return view('welcome');
});
Route::resource('posts', App\Http\Controllers\PostController::class);
Auth::routes();
Thanks in advance
If you want to prevent users to edit posts that they did not create:
public function edit(Post $post)
{
if(Auth::user()->id != $post->user_id) {
throw new \Exception("Access denied", 403);
}
return view('posts.edit', compact('post'));
}
you need to use the Auth class offcourse:
use Illuminate\Support\Facades\Auth;
You need to do the same for the delete function
why you don't the id to appear in url. i can not see the reason to do that because there should be some unique key to edit the post. the only way i see is to add in the database a slug column and put it as unique. when adding a post put the title replacing space with '_' as a slug. make sure it is unique in validation. and in your edit and show code send the slug instead of the id.
route('posts.edit',$post->slug)
don't use dependency injection in controller
public function edit( $slug)
{
$post = Post::where('slug','=',$slug)->first();
return view('posts.edit', compact('post'));
}
hi
I think it will help you
your route will be like this:
Route::resource('posts', App\Http\Controllers\PostController::class)->except([
'edit'
]);
or
Route::resource('posts', App\Http\Controllers\PostController::class, ['only' => ['index', 'destroy', 'update' , 'create' , 'store']]);
or even you can write each route separately...
and write your edit route, separate :
Route::post('/post/edit', [App\Http\Controllers\PostController::class, "edit"])->name('post.edit');
delete #method('DELETE') from your blade and add this input to your blade :
<input type="hidden" name="post_id" value="{{$post->id}}" />
and change this :
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
to this :
public function edit(Request $request)
{
$post_id = $request->post_id;
//write your code here
return view('posts.edit', compact('post'));
}
I'm kind of new using laravel.
I created delete function but it doesnt work as I wanted to. Here are the codes in view, controller as well as routes. Could you guys tell me what was wrong in the code? Thanks
View:
<div class="btn-group">
<a class="btn btn-info" href="{{ URL::to('/delete_data_tanah/{id}') }}">
<i class="fa fa-close"
onclick="return confirm('Are you sure you want to delete this data?');">----</i>
</a>
</div>
Controller:
public function delete($id){
\App\Tbl_object::where('id_objects', '=', $id)->delete();
return redirect('/list_tanah')->with('Success', 'Data telah dihapus');
}
Routes:
Route::post('/delete_data_tanah/{id}', 'formulir_tanah#delete');
Controller:
class formulir_tanah extends Controller
{
public function index()
{
$query_tanah = \App\Tbl_object::where('id_objects_referencies', '=', '1')->get();
$query_view = \App\Tbl_view::where('id_objects_referencies', '=', '1')->get();
$data = ['page_title' => 'Kertas Kerja Penilaian Tanah', 'query_tanah' => $query_tanah, 'query_view' => $query_view];
return view('admin/list_tanah')->with($data);
}
}
Try this:
in your blade.php, change {id} to {$data->id}
<div class="btn-group">
<a class="btn btn-info" href="{{ URL::to('/delete_data_tanah/{$data->id}') }}">
<i class="fa fa-close"
onclick="return confirm('Are you sure you want to delete this data?');">----</i>
</a>
then in your controller, add the id variable:
class formulir_tanah extends Controller { public function index(){
//ambil semua data dari table categories
$query_tanah = \App\Tbl_object::where('id_objects_referencies', '=', '1')->first();
$query_view = \App\Tbl_view::where('id_objects_referencies', '=', '1') ->first();
$data = ['id' => $query_view->id, 'page_title' => 'Kertas Kerja Penilaian Tanah', 'query_tanah' => $query_tanah, 'query_view' => $query_view ];
return view('admin/list_tanah')->with($data);
}
your code doesn't work since laravel doesn't know what row to delete since there is no specific id in your return.
Apply these changes to your code:
Route::delete('/delete_data_tanah/{id}', 'formulir_tanah#delete');
and in your view you need to wrap the button in a form
<form action="{{ URL::to('/delete_data_tanah/{id}') }}" method="post">
#method('DELETE')
#csrf
<button class="btn btn-danger" type="submit">Delete</button>
</form>
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']);
}
I have order system and when user order some product he then can leave feedback for the product.
My issue is when there are multiple products in same order. I want to place button on each product leave feedback.
In my controller I have this
public function orderView($orderId)
{
$order = Order::where('order_id', $orderId)->where('status', 1)->where('user_id', Auth::user()->user_id)->first();
$reviews = Review::where('user_id', Auth::user()->user_id)->get();
return View::make('order_details', [
'order' => $order,
'reviews' => $reviews
]);
}
The function query the orders table and reviews table. Then on the page I'm trying to do this
#foreach($reviews as $review)
#if($item->product_id == $review->product_id)
#if($review->rating_published == 0)
<a class="btn btn-warning" href="">Wating for Approval</a>
#else
<a class="btn btn-warning" href="">Edit Review</a>
#endif
#else
<a class="btn btn-warning" href="">Leave Review</a>
#endif
#endforeach
For now the problem is that if there is no reviews from logged user I don't see "leave review" button on page.
My review model have
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'user_id');
}
public function item()
{
return $this->belongsTo('App\Product', 'product_id','product_id');
}
Product model
public function reviews()
{
return $this->hasMany('App\Review', 'product_id');
}
you can make use of #forelse blade syntax
#forelse ($users as $user)
<li>{{ $user->name }}</li>
#empty
<p>No users</p>
#endforelse
so your code may be rewritten as follows
#forelse($reviews as $review)
#if($item->product_id == $review->product_id)
#if($review->rating_published == 0)
<a class="btn btn-warning" href="">Wating for Approval</a>
#else
<a class="btn btn-warning" href="">Edit Review</a>
#endif
#endif
#empty
<a class="btn btn-warning" href="">Leave Review</a>
#endforelse
so if the review array is empty it will show the leave a review 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');
}