Laravel 8 Copy every page relaod - php

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>

Related

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

The value does not change in the database

I made a button to update a user's access to the website and I get an error. I searched here but I did not find the answer to help me, can you give me some advice?
This is the error: Creating default object from empty value
Controller:
public function suspendUser(Request $request, $id) {
$user = User::find($id);
$user->userAcces = 0;
$user->save();
return back();
}
View::
<div class="col-md-3 col-sm-3">
<form>
<i class="fa fa-eye" aria-hidden="true"></i>
<i class="fa fa-ban" aria-hidden="true"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-user-friends"></i>
<form>
</div>
Route:
Route::get('/updateUser/{id}', 'UserController#suspendUser');
I tried other things but failed, maybe something went wrong in the controller?
It means this user is not found with this $id and $user is null , so check the $id which sent to the suspendUser method, and it's better to use User::findOrFail($id); in order to return 404 if model not found ,and not face these kinds of errors.

How can i delete particular attribute and return to the home page in laravel 5.6

When I press the delete button nothing happens. What am I doing wrong?
My html code is:
<div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
<a rel="{{ $attribute ->id }}" rel1="delete-attribute" href="javascript:" class="dropdown-item">Delete</a>
</div>
public function deleteAttribute($id = null) {
ProductAttribute::where(['id'=>$id])->delete();
return redirect()->back()->with();
}

Download .PDF at the next page after successful form insertion in Laravel

I have a form, that user has to fill out. After pressing submit, the user will be redirected to page called /medicines/result with a success message and download button for the .PDF. I cannot figure out how to download the pdf on the download page. If I use the pdf method in the controller, it works fine.
I am using this for pdf: barryvdh/laravel-dompdf
What I have:
public function store(StoreConfirmationRequest $request) {
$formData = $request->all();
$prescription_confirmation = new Prescription_confirmations;
$prescription_confirmation->physician_id = Input::get('physician_id');
$prescription_confirmation->patient_code = Input::get('patient_code');
$prescription_confirmation->medicine = 1;
$prescription_confirmation->date = Input::get('physician_date');
//finally save the data to database
$prescription_confirmation->save();
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
$pdf->download();
return view('medicines.result', compact('formData'))->with('message', 'Successfully inserted);
}
Here is the blade
#extends ('layouts/master')
#section('title', 'Allalaadimine')
#section ('content')
<div class="container medicine_confirmation_container">
<div class="col-md-12">
#foreach ($formData as $data)
<li>{{$data}}</li>
#endforeach
<div class="flash-message">
#if(session()->has('message'))
<div class="alert-message alert-message-success">
<h4>Saved!</h4>
{{ session()->get('message') }}
</div>
#endif
</div> <!-- end .flash-message -->
<button class="btn btn-borders btn-success btn-lg btnDownloadPdf" type="submit">Lae alla pdf</button>
</div>
</div>
#endsection
#section ('footer')
#endsection
Returning both the PDF and a view won't work. You have to save the PDF on your server and make it accessible to download by another route.
From: https://github.com/barryvdh/laravel-dompdf
$pdf->save('/path-to/my_stored_file.pdf');
In the blade template you have to link your saved PDF.

Use delete glyphicon in Laravel 5 to delete a data

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.

Categories