delete all that are checked in laravel - laravel

Hello I want to delete all the rows from the database which are being checked by the user but whenever I do this I get this error "Missing argument 1 for App\Http\Controllers\UploadController::deleteSelectedUploads()". I know why this error is coming because I haven't passed any id to it and if I pass any id to it then it says Undefined variable: upload. Please help me out. I know I have been posting a lot of questions nowadays and later solve it on my own but this is the only way of learning I guess.
Controller
public function deleteSelectedUploads($id) {
//$uploads = $this->upload->get($id);
// $checkboxes = $this->request->input('checkbox[]');
// if((Auth::user()->role=='admin')) {
// $this->upload->deleteUpload($id);
// } else {
// Session::flash('message', "Oooopsss ... ! seems like you don't have the authority to
// <span style=font-weight:bold;'><u>DELETE</u></span> this file.
// <span style='font-size:15pt; font-weight:bold;'> Contact ADMIN </span>");
// return redirect('home');
// }
$checkbox = Input::all();
foreach($checkbox['checkbox'] as $id) {
Upload::where('id', $id)->delete();
}
Session::flash('message', 'You File has been deleted');
return redirect('home');
}
View
<th><form action="/delete-selected-uploads" method="get">
<input type="checkbox" name="checkbox" onClick="toggle(this)"/>
<input type="submit" class="btn btn-block btn-success btn-xs" value="Delete all checked">
</form>
</center>
</th>
<td>
<input type="checkbox" name="checkbox[]" id="{{ $upload['id'] }}" value="{{$upload['id']}}">
</td>
Route
Route::get('/delete-selected-uploads', 'UploadController#deleteSelectedUploads');

Your problem is that you maybe put the parameter to
The function public function deleteSelectedUploads($id) { it need the parameter $id.
And you call this function avoid ::deleteSelectedUploads().
One solutions can be put not requerid the $id parameter as public function deleteSelectedUploads($id = '') {
UPDATE: Try in the view:
<th>
<form action="/delete-selected-uploads" method="get">
<input type="checkbox" name="checkbox" onClick="toggle(this)"/>
<input type="submit" class="btn btn-block btn-success btn-xs" value="Delete all checked">
<td>
<input type="checkbox" name="checkbox[]" id="{{ $upload['id'] }}" value="{{$upload['id']}}">
</td>
</center>
</th>
</form>
I hope help you.

Related

How to update specific row using modal Laravel 5.8

I am trying to update a specific row using modal. However I don't have any idea how to pass the value of the row id to the route parameter.
Here's the update form.
<form action="{{route('subcategory.update', 'idhere')}}" method="POST">
#method('PATCH')
#csrf
<div class="modal-body">
<label for="editname">New sub-category name:</label>
<input type="text" name="editname" id="editname" class="form-control">
<input type="text" name="editid" id="editid" value="" hidden>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="submit">Save changes</button>
</div>
</form>
Edit button
<button class="btn btn-secondary btn-sm" data-myid="{{$item->id}}"
data-mytitle ="{{$item->name}}"
data-target="#editsub" data-toggle="modal">Edit</button>
And here's the controller
public function update(Request $request, Subcategory $subcategory)
{
// return $request;
Subcategory::where('id',$request->editid)->update([
'name' => $request->editname
]);
return back();
}
When you pass an id here and if your model name is subcategory.php you will get the db record in your controller.
<form action="{{route('subcategory.update', $subcategory )}}" method="POST">
Or you can do following too
<form action="{{route('subcategory.update', $subcategory->id )}}" method="POST">
Your route should have {subcategory} in it for eager loading. After that you can do following in your code.
public function update(Request $request, Subcategory $subcategory)
{
//variable $subcategory has the db record
$subcategory->update(['name' => $request->only('editname')]);
return back();
}
If I understand it correctly, you can store the item id in
<input type="text" name="editid" id="editid" value="{$item->id}" hidden>
And since you are using POST request, you can easily get the item id from the request.
Like the one in your post:
$request->input('editid');
Please try first to pass id in input box hidden field

How do I pass values from views to controller

In a view, When we route a button to a function inside the controller, how can we pass two or more values from present during that view.
I was practicing creating a result management system of students. In the view routed from index of ResultController, we have link options to view mark sheet of class ..or individual student. When we click on select class, it redirects to a view where there is two dropdowns to choose the class and batch of students. When we choose respected class and batch, the values class_id and batch_id is routed to function result inside ResultControler, we select students from that class and batch.. and respected subjects and return a view. In that view, we show the marksheet of students(if theres one), and below I have included a button to add marks/create marksheet.
But, I am so confused how I can pass those class_id and batch_id to create function inside ResultController, from the button.
public function index()
{
return view('resultmainpage');
}
public function choose()
{
$classes= Sclass::all();
$batches= Batch::all();
return view('chooseclassbatchresult',compact('classes','batches'));
}
public function result(Request $request)
{
$classid = $request->class;
$batchid = $request->batch;
//dd($batchid);
$students =Student::where('sclass_id',$classid)
->where('batch_id', $batchid)
->whereHas('subject')
->get();
$class= Sclass::findOrFail($classid);
return view('showstudentresult',compact('students','class','classid','batchid'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
// I need class_id and batch_id here
// dd($classidd);
$students = Student::where('sclass_id',$classid)
->where('batch_id',$batchid)
->whereDoesntHave('subject')
->get();
//dd($students);
Route:
Route::get('/rms','MainPageController#index')->name('rms');
Route::get('results/choose','ResultController#choose')->name('chooseresult');
Route::post('/showstudentresult','ResultController#result')->name('showstudentresult');
Route::resource('results','ResultController');
chooseclassbatchresult.blade.php
#extends('layout')
#section('content')
<h1>Please Choose the Class and Respected Batch Of Student For Result</h1>
</br>
</br>
<form action="{{route('showstudentresult')}}" method="post">
#csrf
<p>
<label>Class Name</label>
<select name='class'>
#foreach($classes as $class)
<option value="{{$class->id}}">{{$class->name}}</option>
#endforeach
</select>
</br>
</p>
<p>
<label>Batch</label>
<select name='batch'>
#foreach($batches as $batch)
<option value="{{$batch->id}}">{{$batch->batch}}</option>
#endforeach
</select>
</p>
</br>
<input type="submit" value="View">
</form>
</br>
</br>
</br>
<h1>OR</h1>
<h3>
<button><a href={{route('students.create')}}>Add New Student</a></button>
</h3>
#endsection
Showstudentresult.blade.php
#extends('layout')
#section('content')
<table border=1>
<thead>
<tr>
<th>S.N</th>
<th>Name</th>
<th>Roll NO</th>
#foreach($class->subjects as $subject)
<th>{{$subject->name}}</th>
#endforeach
<th>Total Mark</th>
<th>Percentage</th>
<th>Division</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $id = 1; ?>
#foreach($students as $student)
<tr>
<td><?php echo $id;?></td>
<td>{{$student->name}}</td>
<td>{{$student->roll}}</td>
#foreach($student->subjects as $subject)
<th>{{$subject->pivot->mark}}</th>
#endforeach
<td>{{$student->result->total_mark}}</td>
<td>{{$student->result->percentage}}</td>
<td>{{$student->result->division}}</td>
<td>
<table>
<tr>
<td>
<button>Edit</button>
</td>
<td>
<form action="{{route('students.destroy',$student->id)}}" method="post">
#csrf
#method('DELETE')
<input type="submit" value="Delete"
onclick="return confirm('Are you sure you want to delete the student?')">
</form>
</td>
</tr>
</table>
</td>
</tr>
<?php $id++ ?>
#endforeach
</tbody>
</table>
</br>
</br>
<button><a href={{results.create}}>Create New</a></button>
#endsection
As Ross Wilson suggested in comment
I would suggest creating a separate page similar to
chooseclassbatchresult with a form that submits the data to create
Add a route in your routes files like:
Route::post('/createPage', 'ResultController#createPage')->name('createPage');
In ResultController add the following function:
public function createPage(Request $request)
{
// Get your required ids here
$classid = $request->class;
$batchid = $request->batch;
//dd($classid);
//dd($batchid );
}
In your chooseclassbatchresult view add another form like below
<form action="{{ route('createPage') }}" method="post">
#csrf
<p>
<label>Class Name</label>
<select name='class'>
#foreach($classes as $class)
<option value="{{$class->id}}">{{$class->name}}</option>
#endforeach
</select>
</br>
</p>
<p>
<label>Batch</label>
<select name='batch'>
#foreach($batches as $batch)
<option value="{{$batch->id}}">{{$batch->batch}}</option>
#endforeach
</select>
</p>
</br>
<input type="submit" value="View">
</form>
Thank you for your response. I got a way around my question. I learned that you can use input type="hidden" to carry those values back to the controller.
Create a route:
Route::post('/create_res', 'ResultController#create_res')->name('results.create_res');
In the View, chooseclassbatchresult.blade.php
<form action="{{route('results.create_res')}}" method="POST">
#csrf
<input type="hidden" name="classid" value="{{$classid}}">
<input type="hidden" name="batchid" value="{{$batchid}}">
<input type="submit" value="Create Mark Sheet">
</form>
In the Result Controller;
public function create_res(Request $request){
$classid = $request->classid;
$batchid = $request->batchid;
$students = Student::where('sclass_id',$classid)
->where('batch_id',$batchid)
->whereDoesntHave('subject')
->get();
$classes= Sclass::findOrFail($classid);
//dd($students);
return view('addmarksheet',compact('students','classes'));
}

Laravel Cannot retrieve a specific record from a form

I am outputting a list from a database table 'books' like this:
function edit()
{
$books = Book::all();
return view('layouts/editbooks', ['books' => $books]);
}
And displaying them like this:
#extends('layouts.master')
#section('title')
#section('content')
<h1>Edit Book</h1>
<form action="{{url('editbook')}}" method="GET">
{{ csrf_field() }}
#foreach ($books as $book)
<div>
<label>{{$book->title}}</label>
<input type='radio' value='{{$book->id}}' name='books[]'/>
</div>
#endforeach
<input type="submit" class="btn btn-warning form-control" value="Edit">
</form>
#endsection
I then want user to choose which record they want to edit by selecting a radio button, and upon clicking a submit button, redirect user to a new page with details of a book.
Therefore I am trying to get a book id from radio button and then if id matches, display everything from that record:
function editing(Request $request)
{
$edit = $request->books;
return view('layouts/editing', ['edit' => $edit]);
}
function updateEdit()
{
$books = DB::table('books')->where('id', $edit)->first();
}
And that is displayed in a view:
#extends('layouts.master')
#section('title')
#section('content')
<h1>Delete Book</h1>
<form action="{{url('removebook')}}" method="POST">
{{ csrf_field() }}
<div>
<input name="name" type="textbox" value="{{ old('name', $edit['name']) }}"/>
</div>
<input type="submit" name="submitBtn" value="Delete Book">
</form>
#endsection
However I get an error message saying:
Undefined index: name (View: C:\xampp\htdocs\laraveladvweb\resources\views\layouts\editing.blade.php)
What is causing the issue?
Isn't $edit just the id of the book?
try this
function editing(Request $request)
{
$edit = $request->books;
$book = DB::table('books')->where('id', $edit)->first();
return view('layouts/editing', ['edit' => $book]);
}
#extends('layouts.master')
#section('title')
#section('content')
<h1>Delete Book</h1>
<form action="{{url('removebook')}}" method="POST">
{{ csrf_field() }}
<div>
<input name="name" type="textbox" value="{{ old('name', $edit['name']) }}"/>
</div>
<input type="submit" name="submitBtn" value="Delete Book">
</form>
#endsection

how to handle dynamically created forms laravel 5.4

I want to know how to fetch name of checkboxes from dynamically created form. First I am displaying each records of data in a separate form then If records need to be deleted we will be able to delete. I am able to give different name as somename+id. But how do I know which name is going in the controller. As It is not clear that which name is going in the controller, I am not able to delete the record. I am doing it in laravel 5.4. Here is my code -
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td>
<span class=""><input type="checkbox" name="deletecolor[{{$color->id}}]" value="{{$color->id}}"></span>
</td>
<td>
<div style="background:{{$color->web_color}}">a</div>
</td>
<td>{{$color->color_name}}</td>
<td>
<button type="submit" class="btn btn-danger">
Delete
</button>
</td>
</form>
</tr>
#endforeach
#endif
then on form submission I want to fetch which I am doing like this -
public function destroy(Request $request)
{
//
$id = $request->input('deletecolor');
$affected = DB::update("DELETE FROM vehicle_color where id = ?", [$id]);
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
I see you have $color->id. Why not delete them based on that?
#if (isset($allcolors))
#foreach ($allcolors as $color)
<tr>
<form method="post" action="/delete">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<td><span class=""><input type="checkbox" name="deletecolor[{{ $color->id }}]" value="{{$color->id}}"></span></td>
<td><div style="background:{{$color->web_color}}">a</div></td>
<td>{{$color->color_name}}</td>
<td><button type="submit" class="btn btn-danger" data-toggle="modal" data-target="#colorDelPopup">Delete</button></td>
</form>
</tr>
#endforeach
#endif
And in your controller:
public function destroy(Request $request)
{
$ids = $request->input('deletecolor');
// Instead of raw SQL, you can use the query builder to make your life a bit easier
$affected = DB::table('vehicle_color')->whereIn('id', $ids)->delete();
//echo $affected==1?"Successfully Deleted":"Delete Fail";
}
This should do the trick.

Codeigniter Validation for Multidimensional Array Data

I having a problem to validate multiple row data in Codeigniter by using form validation. Below code are ok to validate the 1st row of data. The subsequent row data i added via javascript was not able to validate. After i clicked submit, the next row that i disappeared. Anyone can help to this?
Appreciate and thanks in advance for any help.
HTML
<!-- Form -->
<?php echo form_open(base_url('air'), array('class'=> 'form-horizontal','id'=> 'calc_form')) ?>
<table>
<tbody id="item-row">
<tr>
<td>1</td>
<td><input class="form-control" type="text" name="load[1][grossweight]" value="<? =set_value('load[1][grossweight]')?>" placeholder="Gross Weight" >
<p><?=form_error('load[1][grossweight]'); ?></p>
</td>
</tr>
</tbody>
</table>
<input class="btn btn-success" onclick="addRow(this.form);" type="button" value="Add Item" /></strike>
<input id="getrates" type="submit" class="btn btn-primary btn-lg" name="getrate" value="Get Rate" />
</form>
Javascript Jquery
<script type="text/javascript">
var rowNum = 1;
function addRow(frm) {
rowNum ++;
var row = '<tr id="rowNum'+rowNum+'"><td>'+rowNum+'</td><td><input class="form-control" type="text" name="load['+rowNum+'][grossweight]" value="<?=set_value('load[rowNum][grossweight]')?>" placeholder="Gross Weight" ></td></tr>';
jQuery('#item-row').append(row);
}
function removeRow(rnum) {
jQuery('#rowNum'+rnum).remove();
}
</script>
Codeigniter Controller
// Posted array
$loads = $this->input->post('load');
if(!empty($loads))
{
foreach($loads as $id => $data)
{
$this->form_validation->set_rules('load['. $id .'][grossweight]', 'Gross Weight', 'required');
}
}
if ($this->form_validation->run() === FALSE)
{
$this->load->view('air/index', $data);
}

Resources