Please what am I doing wrong, Paymentdetails refuse to delete instead, its loading server error.There's something wrong with my code.
This is my controller
public function destroy(Paymentdetail $paymentdetail)
{
// dd($id);
if(Auth::user()->role_id == '1'){
$this->paymentdetails->delete($paymentdetail->id);
event(new Deleted($paymentdetail));
return redirect()->route('paymentdetails.index')
->withSuccess(__('Payment details deleted successfully.'));
}
else{
return redirect()->back()
->withErrors(__('Sorry! You Are Not Authorized To Delete Payment Details.'));
}
}
index.blade
<a href="{{ route('paymentdetails.edit', $paymentdetail) }}"
class="btn btn-icon edit"
title="#lang('Edit Paymentdetail')"
data-toggle="tooltip" data-placement="top">
<i class="fas fa-edit"></i>
</a>
<a href="{{ route('paymentdetails.destroy', $paymentdetail) }}"
class="btn btn-icon"
title="#lang('Delete Paymentdetail')"
data-toggle="tooltip"
data-placement="top"
data-method="DELETE"
data-confirm-title="#lang('Please Confirm')"
data-confirm-text="#lang('Are you sure that you want to delete this payment details?')"
data-confirm-delete="#lang('Yes, delete details!')">
<i class="fas fa-trash"></i>
</a></a>
Moved my answer from comments"
$this->paymentdetails->delete($paymentdetail->id);
seems over-engineered and confusing
$paymentdetail is instance of Paymentdetail and it extends Laravel Model. As it is instance of model - it knows how to find itself in DB therefore you can call
$paymentdetail->delete();
and instance will delete itself
First check your request method,it should be Get method.
iF method is Get then is it redirecting to given function?
You should on debug mod of laravel which gives you proper error.
Another thing check is user authenticated?
Related
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.
I would like to ask if it is possible to validate a href element in laravel.
For example, I'm having a bunch of people responses in my guest book and each user is able do delete his posts. I'm deleting it like this:
<div class="col-lg-2">
<ul class="list-unstyled trash">
<li>
<a href="/responses/{{$response->id}}">
<span onclick="alertDialog()" class="glyphicon glyphicon-trash"></span></a></li>
<li><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#{{$response->id}}">Edit</button></li>
</ul>
</div>
Unfortunately, every single user can change "/responses/{{$response->id}}"> id via browser developer tools and delete other user's responses. Is there any possible solutions to prevent this issue?
Just check the logged user before rendering that html section:
<div class="col-lg-2">
<!-- assuming post is your variable and user is a property which references the user who created the record -->
#if($post->user == Auth::id())
<ul class="list-unstyled trash">
<li>
<a href="/responses/{{$response->id}}">
<span onclick="alertDialog()" class="glyphicon glyphicon-trash"></span></a></li>
<li><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#{{$response->id}}">Edit</button></li>
</ul>
#endif
</div>
This way only the user who owns the post will be able to see the button in the html.
You need to use policies to check if the user that tries to delete a record is its owner:
public function delete(User $user, Model $model)
{
return $user->id === $model->user_id;
}
Or you can do it manually in a controller method:
if (auth()->id() === $model->user_id) {
// Delete the record
}
You should make /responses/{{$response->id}} a POST route and verify that the passed id matches with the authenticated users id. ( Auth::user()->id;)
I ended up checking like this in my destroy(id) method:
$count = Comment::where(['response_id' => $id])->count();
if (($responses->where('id',$id)->value('guests_email') == Cookie::get('email') && ($count==0)) || (Auth::check()))
I have this route
Route::name('recruitment.edit.profile')->get('/edit-profile/{id}', 'EmployeeController#showEditProfileForm');
having a parameter id and
this code in my blade.php
<a href="{{ route('recruitment.edit.profile', $users->id) }}" class="btn btn-success">
<i class="fa fa-edit m-right-xs"></i> Edit Profile
</a>
But when I clicked the given link I got this url http://mpci-vo.dev/recruitment/edit-profile?9 and a message Whoops, looks like something went wrong.
My question why is there a ? before 9
Change route('recruitment.edit.profile', $users->id)
to
route('recruitment.edit.profile', ['id' => $users->id])
https://laravel.com/docs/5.4/helpers#method-route
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.
I'm trying to add a Session success message when a User login.
I've tried adding the following to the AuthenticatesUsers.php trait postLogin():
if (Auth::attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles)->withSuccess("message");
}
I've also tried adding to the handleUserWasAuthenticated():
return redirect()->intended($this->redirectPath())->withSuccess("message");
I run composer dump-autoload after each change but it just will not flash the message in the view. I use a partial called success.blade.php and the contents are:
#if (Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>
<i class="fa fa-check-circle fa-lg fa-fw"></i> Success.
</strong>
{{ Session::get('success') }}
</div>
#endif
I think I'm missing something but I can't think what at the moment so hoping for a fresh set of eyes.
Thank you in advance.
Don't use ->withSuccess().
Use ->with('success', 'Success message'), as described in http://laravel.com/docs/5.1/responses#redirecting-with-flashed-session-data, or use the session manager. To access the session manager, you can use the Request object:
$request->session()->flash('success', 'Success message');
See http://laravel.com/docs/5.1/session#flash-data. You can also access the session manager using the Session facade:
Session::flash('success', 'Success message');