I'm new to laravel and I'm having an issue deleting a user.
I cannot get the id of the user I wish to delete.
Any help is appreciated.
view
<form method="post" action="/staff/{{$user->id}}">
<input type="hidden" name="_method" value="DELETE">
{{csrf_field()}}
<button style="padding: 0" type="submit" class="btn btn-link margin-left-40"
onclick="return confirm('Are you sure you want to delete {{ucfirst($user->name)}}?');">
<i class="icmn-bin"></i> Delete</button>
</form>
controller
public function destroy(User $user)
{
$thisuser = User::find($user->id);
$thisuser->delete();
return redirect('/staff');
}
route
Route::resource('/staff', 'User\UserController');
Please try
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}
<form method="post" action="{{route('staff.destroy',$user->id)}}">
<input type="hidden" name="_method" value="DELETE">
{{csrf_field()}}
<button style="padding: 0" type="submit" class="btn btn-link margin-left-40"
onclick="return confirm('Are you sure you want to delete
{{ucfirst($user->name)}}?');">
<i class="icmn-bin"></i> Delete</button>
</form>
//your route
Route::get('/staff/{id}/delete', 'User\UserController#destroy')->name('staff.destroy');
//method
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}
In addition to Kuldeep Mishra's solution, make sure your form is properly bound to the user you intend to delete.
You should try this:
public function destroy($id)
{
//Soft delete
$thisuser = User::destroy($id);
//OR
//Permanent Delete
$thisuser = User::where('id',$id)->delete();
return redirect('/staff');
}
html
<form method="post" action="/staff/{{$user->id}}">
<input type="hidden" name="_method" value="DELETE">
{{csrf_field()}}
<button style="padding: 0" type="submit" class="btn btn-link margin-left-
40" onclick="return confirm('Are you sure you want to delete
{{ucfirst($user->name)}}?');">
<i class="icmn-bin"></i> Delete</button>
</form>
route
Route::delete('/staff/{id}', 'UserController#destroy');
function
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}
Related
I have several buttons to insert data into the database, but only one command works, while the others don't work, what should I do, I will give an example of the data I created
this is my controller :
public function store(Request $request)
{
//
$days = $request->hari;
$hours = $request->jam;
$minutes = $request->menit;
$model = new Q_Topic_User;
$model->status = $request->status;
$model->topic_id = $request->topic_id;
$model->timer = Carbon::now()->addDays($days)->addHours($hours)->addMinute($minutes);
$topik = $request->topic_id;
$model->user_create = auth()->id();
// dd($request->all());
$model->save();
return redirect('qanswers/'. $topik);
}
public function storeAns(Request $request)
{
$modellama = Q_Answer::where('user_create', '=', $request->kode_user)
->Where('topic_id', '=', $request->topic_id)
->Where('question_id', '=', $request->question_id);
$a = $request->user_answer;
$b = $request->answer;
if ($a === $b){
$nilai = "1";
}
else{
$nilai = "0";
}
if ($modellama >= "1"){
$modellama->delete();
}
// dd($request->all());
$model = new Q_Answer;
$model->topic_id = $request->topic_id;
$model->question_id = $request->question_id;
$model->user_answer = $request->user_answer;
$model->answer = $request->answer;
$model->hasil = $nilai;
$model->user_create = auth()->id();
$topik = $request->topic_id;
$model->save();
return redirect('qanswers/'. $topik);
}
public function storeTot(Request $request)
{
$tot_nilai = Q_Answer::select(DB::raw("CAST(SUM(hasil) as int) as tot_nilai"))
->Where('topic_id', '=', $request->topic_idi)
->Where('user_create', '=', auth()->id())
->pluck('tot_nilai');
// dd($tot_nilai);
$tot_soal = Q_Question::select(DB::raw("CAST(COUNT(topic_id) as int) as tot_soal"))
->Where('topic_id', '=', $request->topic_idi)
->pluck('tot_soal');
dd($request->all());
$model = new Q_Answer_Tot;
$model->topic_id = $request->topic_idi;
$model->totalsoal = $tot_soal;
$model->totalnilai = $tot_nilai;
$model->user_create = auth()->id();
// dd($request->all());
$model->save();
$topik = $request->topic_id;
return redirect('qanswers/'. $topik);
}
blade :
<form method="POST" action="{{ url('qanswers') }}"
enctype="multipart/form-data">
#csrf
#foreach ($datas as $d_value)
<button type="submit" class="btn btn-sm bg-success">Kirim data</button>
#endforeach
</form>
<form method="POST" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
<form method="POST" action="{{ url('qanswers.storeTot') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $t_value)
<button type="submit" class="btn btn-sm bg-success">Kirim Pilihan Peserta</button>
#endforeach
</form>
my web.php :
Route::resource('/qanswers', Q_AnswersController::class);
Route::post('/storeAns',[Q_AnswersController::class, 'storeAns'])->name('storeAns');
Route::post('/storeTot',[Q_AnswersController::class, 'storeTot'])->name('storeTot');
i can save data in store but i can save data using storeAns and storeTot. I dont find error in laravel log but i can see my data using dd($request->all()); can u help me?
Its good practice to give each form an unique id. With multiple forms and buttons, I would specify the form for the button. And you need to use value to figure out which button was pressed, especially when you are going to the same url in more than one form.
<form method="POST" id="answers" action="{{ url('qanswers') }}"
enctype="multipart/form-data">
#csrf
#foreach ($datas as $d_value)
<button type="submit" class="btn btn-sm bg-success" value="form1">Kirim data</button>
#endforeach
</form>
<form method="POST" id="storeans1" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success" value="form2">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
<form method="POST" id="storeans2" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success" value="form3">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
I have shown the value with formx. SInce you are creating many buttons for each form, each should have an unique value such as
form1btn1
form1btn2
form1btn3
This function search user from MySQL. Can anyone knows which part I did wrong ?, searchUser function is not triggered.
admin view
<form type="get" action="role-permission-search">
<input type="search" class="" name="query" placeholder="Search"/>
<button type="button" class="btn btn-outline-primary">search</button>
</form>
Route::group(['prefix'=>'admin', 'middleware'=>['isAdmin','auth']], function(){
// Serch users
Route::get('role-permission-search', [AdminController::class, 'searchUsers']);
});
function searchUsers()
{
dd('Where are you');
$search_request = $_GET['query'];
$users = User::where('name', 'LIKE', '%'.$search_request.'%')-get();
return view('dashboards.admins.rolePermission', compact('users'));
}
You can remove type="button" from below line
<button type="button" class="btn btn-outline-primary">search</button>
or change type="submit"
If still that not works for you then you should defile action="{{ route('YOUR ROUTE NAME') }}" in tag
This is my route :
Route::post('/store', 'EditlinkController#store');
My controller
public function index()
{
$id = $_GET['id'];
$links = DB::table('slugs')->where('slugs.id',$id)
->join('links','links.sid','=','slugs.id')
->get();
return view('editlink', ['links' => $links]);
}
public function store(Request $request, $id)
{
$url = $request->input('url');
$data =new link;
$sid = $id;
$data->sid = $sid;
$data ->url = $url;
$data ->save();
return redirect('/links');
}
And my view:
<form role="form" method='POST' action="{{url('store')}}">
<div class="entry input-group col-xs-3">
<input class="form-control" name="url" type='url' placeholder="https://..." size="100"/>
<input type="hidden" name="_Token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary" type="button">
<span class="material-icons md-12">add</span>
</button>
{{ csrf_field() }}
</div>
</form>
So basically here I want to call $id from index to store. I also have tried
Route::post('/store/{id}', 'EditlinkController#store');
But still doesn't work. Thank you.
Your route, /store/{id}, requires you to have a route parameter. Make sure you have an $id available to you before you generate your url for your form.
An example of what your open form tag should look like with the $id included:
<form role="form" method='POST' action="{{url('store', $id)}}">
I'm assuming the view editlink is where the markup for the form resides. If so, then you can simply pass the value of the id to your view from your controller:
return view('editlink', ['links' => $links, 'id' => $id]);
I have a form associated with the destroy method to delete a item/record.
<form action="{{ route('climb-excluded.destroy',$exclude->id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-danger btn-sm">
<i class="fa fa-trash-o" aria-hidden="true"></i> Delete
</button>
</form>
destroy() method
<?php
public function destroy(Cexcluded $cexcluded)
{
$cexcluded->tours()->detach();
$cexcluded ->delete();
Session::flash('success','Item sucessfully deleted !');
return redirect()->route('climb.ie');
}
Routes
Route::resource('climb-excluded','CexcludedsController', ['only'=>['store','destroy']]);
Route::get('climbing/included-excluded','HomeController#getclimbIE')->name('climb.ie');
The trouble I'm having is the destroy method is not deleting the record from the DB and doesn't give any error. It is giving a session message without deleting the record.
I saw your other thread with the same issue regarding the detach causing problems.
Add this to your Cexcluded model
public static function boot()
{
parent::boot();
static::deleting(function($model) {
$model->tours()->detach();
});
}
Remove the following line from your controller method
$cexcluded->tours()->detach();
So try this
public function destroy(Cexcluded $cexcluded)
{
$cexcluded ->delete();
Session::flash('success','Item sucessfully deleted !');
return redirect()->route('climb.ie');
}
You must use "->destroy()" in the controller.
Check this question for clarification between destroy() and delete()
Destroy method not working
I am trying to delete an image from my database but I am receiving the following error:
Undefined variable: portfolio (View: C:\MyProjects\bubblehouseProject\resources\views\admin\portfolio\addPortfolio.blade.php)
I believe there is something wrong with the logic I am trying to implement in my Laravel application. I am using Eloquent ORM.
Controller:
public function Destroy($id) {
$portfolio = new Portfolio();
$portfolio::find($id);
$portfolio->delete();
return redirect('addPortfolio')->with('delete', 'The image has been successfully deleted!');
}
Route:
Route::get('addPortfolio/{id}', 'AddPortfolioController#Destroy');
View:
<form action="{{ route('addPortfolioController.Destroy', $portfolio->id) }}">
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
{{ method_field('DELETE') }}
</form>
I am not entirely sure that this is the right way to delete data from the database.
Route
Route::delete('addPortfolio/{id}', 'AddPortfolioController#Destroy')->name('portfolio.destroy');
View
<form action="{{ route('portfolio.destroy', $portfolio->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
</form>
Controller
public function destroy($id)
{
if(Portfolio::destroy($id)) {
return redirect('addPortfolio')->with('success', 'The image has been successfully deleted!');
} else {
return redirect('addPortfolio')->with('error', 'Please try again!');
}
}