Use delete glyphicon in Laravel 5 to delete a data - php

Hi need help in glyphicons with delete functionality.
These are my icons for update and delete. However, I only have my update working. I do not do with my delete. Please help! THanks a lot!
</span>
<span class="glyphicon glyphicon-trash"></span>
Controller codes for update ticket:
where Chap_ticket is my database table name
public function editTicket($id)
{
$tick = Chap_ticket::find($id);
$tickets=Chap_ticket::all();
return view('admin.registerTicket',compact('tick','tickets'));
}
My Route:
Route::get('admin/editTicket/{id}','Admin\AdminController#editTicket');
Route::get('admin/deleteTicket', 'AdminController#deleteTicket');
Route::get('admin/registerTicket','Admin\AdminController#registerTicket');

You need to create a new controller action like e.g. below:
public function deleteTicket($id) {
$tick = Chap_ticket::find($id);
$tick->delete();
return Redirect::back()->with('msg', 'Ticket deleted');
}
You may need to associate the new action with a route so you can add the following in your routes:
Route::get('admin/deleteTicket/{id}', 'Admin\AdminController#deleteTicket');
Assuming your controller is called AdminController
And in your view:
#if (isset($msg))
<div>
{{$msg}}
</div>
#endif
</span>
<span class="glyphicon glyphicon-trash"></span>
This is based on the assumption that your view is a general admin page which will still be valid to when the ticket is deleted.

Related

Laravel 8 Copy every page relaod

I have a little problem, my system carries out the copying process when I click on my copy button, but now when I use ex. then wants to reload the page on the new page because for some reason (internet, etc.) the page does not load properly, it copies the same entry again.
Can you somehow block or intercept that this is only triggered when the button is clicked?
Here ist my code snippet:
My UserController.php
public function pduplicate ($id)
{
$user = User::find($id);
$newUser = $user->replicate(["COLUMS_TO_NOT_COPY", "id", "created_at", "updated_at"]);
$newUser->save();
$users = User::all();
$newId = $newUser->id;
return view('users.edit', compact('users', 'newId'));
}
web.php
Route::get('/home/pedit/{id}', [App\Http\Controllers\UserController::class, 'pduplicate']);
View Trigger Button
<div class="col-sm-2">
<a class="btn btn-primary" href="/home/pedit/{{ $users->id }}">
<i class="fas fa-copy"></i> Person kopieren
</a>
</div>

Cannot find the way to make #if statement works in blade

I'm making a "teacher's" app, and I want to make a log-in page which changes depending if there's registered users in the database or not.
I want to make a redirection button to a create user page if there aren't auth users in database, and to make a select user view if the database have one or more users.
The problem is that I don't know how to exactly do this, 'cause the view always shows me the first statement (what I've got in the if), also if in the database are registered users. Can anyone help me with this please?
This is the blade file:
#if (empty(Auth::user()->id))
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
Here you have the controller index method:
public function index()
{
$users = User::all();
return view('/', compact('users'));
}
And finally here you have the page:
The following code is the sample for it, kindly replace code accordingly
#if(!$user)
//show button
#else
//dont show button
#endif
I think your question is you want to check if there is user in database.
So no need to check if the user authenticated but to check if there is user on the database.
In your controller
public function index() {
return view('/', ['users' => User::all()]);
}
and in your blade file
#if(!$users)
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
This function will get the current authenticated user: Auth::user(). I guess what you are trying to achieve is #if(empty($users)) where $users is the variable you are passing on controller.
If you want to verify if the user that accessed to that view is authenticated you can simply use #auth and #guest.
Also i would suggest you to change your button to an <a> tag and your href would be <a href="{{ route('route.name') }}" where route.name would be defined in your routes file.
in your controller:
you can create a folder inside views called users and then the index.blade.php (views/users/index.blade.php)
public function index()
{
$users = Users::all();
return view('users.index')->with('users', $users);
}
in your view:
#if(count($users) < 1)
...
#else
...
#endif
count is validating if the $users array length is less then 1 (in other words if the array is empty).
Alternative you can you isEmpty()
#if($users->isEmpty())
...
#else
...
#endif

How to apply loading state only to particular component in livewire

I'm using Laravel Livewire in my project, I use wire:loading for loading the state while clicking. I iterated all the tasks in foreach loop but the loading state applies for all components. Here is the code.
Blade file
GitLab: https://gitlab.com/tasklog/tasklog/-/blob/master/resources/views/livewire/home/tasks.blade.php
<button type="button" wire:click="togglePraise({{ $task->id }}, {{ $task->user->id }})">
👏
<span class="small text-black-50 font-weight-bold">
{{ $task->task_praise->count() }}
</span>
<div wire:loading wire:target="togglePraise">
Processing...
</div>
</button>
Controller file
GitLab: https://gitlab.com/tasklog/tasklog/-/blob/master/app/Http/Livewire/Home/Tasks.php
public function togglePraise($id, $user_id)
{
if (Auth::check()) {
if (Auth::user()->id === $user_id) {
session()->flash('message', 'Forbidden!');
return;
}
$isPraised = TaskPraise::where([
['user_id', Auth::user()->id],
['task_id', $id],
])->count();
if ($isPraised === 1) {
TaskPraise::where([
['user_id', Auth::user()->id],
['task_id', $id],
])->delete();
return true;
} else {
TaskPraise::create([
'task_id' => $id,
'user_id' => Auth::user()->id,
]);
return true;
}
} else {
return session()->flash('message', 'Forbidden!');
}
}
I know the question was before the realease of v2, yet adding the answer for v2 for reference.
as per the Livewire docs if you're using v2, you may specify the action and its parameters in the wire:target directive. For your example, it would be like this:
wire:target="togglePraise({{ $task->id }}, {{ $task->user->id }})"
I was unable to do it by loading targets to actions with parameters so I used jquery with the livewire
Button in the table loop with default d-none loading icon class
<div class="col-3">
<button class="btn btn-sm btn-default btn-save ">
Save <span class="loading-icon d-none">
<i class="fa fa-circle-o-notch fa-spin" style="font-size:14px"></i></span>
</button></div>
Javascript code to call livewire and enable loading
$('.btn-save').click(function (e) {
e.preventDefault();
$('.parameter-value').removeClass("error-field");
var row = $(this).closest('tr');
row.find('.loading-icon').removeClass('d-none');
var parameter = row.data('parameter');
var value = $.trim(row.find('.parameter-value').val())
if(value == ""){
row.find('.parameter-value').addClass('error-field');
}else{
row.find('.parameter-value').removeClass('error-field');
//Livewire call
#this.addParameterValue(parameter,value);
}
});
before LiveWire function ends dispatch browser event
public function addParameterValue($parameterID,$value)
{
...
$this->dispatchBrowserEvent('parameter_value-saved');
}
Handle Browser event from javascript end and remove hide loading icon inside
window.addEventListener('parameter_value-saved', event => {
$('.loading-icon').addClass('d-none');
})
I had the same issue as you. After researching, I decided to make 2 wire:click, IDK if this is the best way to solve this but yeah that's what I do
<div wire:loading wire:target="LoadingAnimation({{$loop->iteration}})">
// icon loading or teks for showing this row load sth
</div>
......
<div wire:click="LoadingAnimation({{$loop->iteration}})">
<a wire:click="setUpdateid({{$author->id}})">
// your content here , teks or maybe an icon
</a>
</div>
If you have any questions let me know, Happy coding!
Edit: Don't forget to make the method inside the class, like the LoadingAnimation and the setUpdateid

how to force delete in laravel 5.4

I made a user management system with soft deletion and force deletion options. However, I'm having trouble getting the force deletion option to work.
The route:
Route::post('users/{user}/delete', 'UserController#forcedelete');
The relevant controller code:
public function forcedelete(User $user)
{
$user->forceDelete();
return redirect('users/trash');
}
The view code:
<a href="{{ url('users/'.$user->id.'/delete') }}"
onclick="event.preventDefault(); document.getElementById('delete').submit();">
<i class="fa fa-trash-o btn btn-danger btn-xs"></i>
</a>
<form id="delete" action="{{ url('users/'.$user->id.'/delete') }}"
method="POST" style="display: none;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
</form>
The error that I'm getting is
MethodNotAllowedHttpException in RouteCollection.php line 233:
Why is it not working, and how can I fix it?
Try placing this route above your other user routes or user resource route. Also you're trying to use route model binding with a soft deleted model, which won't work. You need to use the id and delete it manually.
public function forcedelete($id)
{
User::where('id', $id)->forceDelete();
return redirect('users/trash');
}
Edit: Also delete {{ method_field('DELETE') }} from your form, since the route method defined is post.
Methods to remove/restore records from the table. Laravel 5.x, 6.x, 7.x
To enable soft deletes for a model, use the Illuminate\Database\Eloquent\SoftDeletes trait on the model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
Soft delete:
This will move record to trash
$user= User::find($id);
$user->delete();
Force Delete: Permanently Deleting Models
$user= User::withTrashed()->find($id);
$user->forceDelete();
Restore: Restoring Soft Deleted Models
$user= User::withTrashed()->find($id);
$user->restore();

how to display My table data in Laravel 5.2

I have collaborators table in My Laravel app see this following
I need collaborator_id print in My index.blade.php file who equel with Auth::user()->id to logged with the system. I wrote following code in My Collaboration Model
public function scopeColabo($query){
return $query->where('collaborator_id',Auth::user()->id);}
and this is My ProjectCollaboratorController function
public function index(){
$collaborators = Collaboration::colabo()->getreturn view('collaborators.index')->withCollaboration($collaborators);}
and this is My index.blade.php
<div class="container">
#if($collaboration)
<div class="row">
#foreach ($collaboration as $proj)
<div class="col-md-3" style="border:1px solid #ccc;margin-left:5px;">
<h2>{!! $proj->project_id !!}</h2>
<p>Tasks: 0</p>
<p>Comments: 0</p>
<p>Attachments: 0</p>
</div>
#endforeach
</div>
#endif
#if($collaboration->isEmpty())
<h3>There are currently no Collaboration</h3>
#endif
</div>
But when I click collaboration link index.blade.php file generate
There are currently no Projects
but in My table there is data....how can print collaborator_id in collaboration Table relete with current logged user?
Try to use ->with() instead of ->withCollaboration:
public function index() {
$collaborators = Collaboration::colabo()->get();
return view('collaborators.index')->with(compact('collaborators'));
}
or just pass your data as second parameter:
public function index() {
$collaborators = Collaboration::colabo()->get();
return view('collaborators.index', compact('collaborators'));
}
The problem is with the collaborator_id in the table which is used to log to test the system:
In the tests, in the logging account, data should match with collaborator data, so collaborator_id should match with the logged user id.

Categories