Delete function in view | Laravel - laravel

I have this function to delete only a row from a table in my database.
public function delete($id)
{
DB::table('user')->where('userID', '=', $id)->delete();
return redirect('userAdmin');
}
And I have a button which gets created for every row.
#foreach ($scores as $score)
<tr>
<td>{{ $score->id }}</td>
<td>{{ $score->serialnumber }}</td>
<td>{{ $score->name }}</td>
<td>{{ $score->created_at }}</td>
<td></td>
<td>
<button class="btn btn-danger" type="submit">Delete this Row</button>
{{ csrf_field() }}
</td>
</tr>
How do I get a delete functionality behind it?

In your view:
<form action="{{ route('yourmodel.delete', $score->id) }}" method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Add a route with a name you like, and a parameter for the id. Let that route call the method in your example and you should be good.
There isn't really such a thing as a DELETE function in HTML forms, so you have to spoof it. Take a look at the docs.

In your view:
<form action="{{ route('yourmodel.delete', $score->id) }}" method="post">
{{ csrf_field() }}
#foreach ($scores as $score)
<tr>
<td>{{ $score->id }}</td>
<td>{{ $score->serialnumber }}</td>
<td>{{ $score->name }}</td>
<td>{{ $score->created_at }}</td>
<td></td>
<td>
<button class="btn btn-danger" type="submit">Delete this Row</button>
</td>
#endforeach
</form>
In your routes:
Route::post('Test/{id}', 'TestController#delete')->name('yourmodel.delete');

Related

Route [$products.edit] not defined

#foreach($products as $product)
<tr>
<td>{{ $product->id }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->job_category }}</td>
<td>{{ $product->purpose }}</td>
<td>{{ $product->region }}</td>
<td>{{ $product->email }}</td>
<td>{{ $product->contact }}</td>
<td>Edit</td>
<td>
<form action="{{ route('$products.destroy', $product->id) }}" method="post">
#csrf
#method('DELETE')
<button class="btn btn-danger" type="submit">DELETE</button>
</form>
</td>
</tr>
#endforeach
This is my ProductController
$product = Product::findOrFail($id);
return view('products.edit', compact('product'));
And this is my route
Route::resource('products', 'ProductController');
Just remove $ from route name
here
href="{{ route('$products.edit', $product->id) }}"
//and here :
action="{{ route('$products.destroy', $product->id) }}"
should be
href="{{ route('products.edit', $product->id) }}"
action="{{ route('products.destroy', $product->id) }}"

Laravel. image cannot be display

enter image description herePhoto is in public/image folder
and the image cannot be fetched properly
#foreach($lostitem as $item)
<tr>
<td>{{ $item->LostItemID }}</td>
<td>{{ $item->date }}</td>
<td>{{ $item->TimeFound }}</td>
<td>{{ $item->AreaWhereFound }}</td>
<td><img src="{{ asset('public/images/'.$item->image) }}" alr="image"></td>
<td>{{ $item->Remark }}</td>
<td>{{ $item->UserLevelID }}</td>
<td>{{ $item->DateClaimed }}</td>
<td>{{ $item->TimeClaimed }}</td>
<td>{{ $item->created_at->format('Y-m-d g:i a') }}</td>
<td>{{ $item->updated_at }}</td>
<td>
<a href="{{route('LostItem_edit', $item->code) }}" class="btn btn-sm btn-info">
<i class="fa fa-edit"></i>
</a>
<form method="POST"
action="{{ route('LostItem_delete', $item->code) }}">
{{ csrf_field() }}
{{ method_field('delete') }}
<button class="btn btn-sm btn-danger" onclick="return confirm('Are you Sure?');">
<i class="fa fa-trash"></i>
</button>
</form>
</td>
</tr>
#endforeach
this is the code is used in the view to fetch the photo
You have already used asset() so you dont have to write public inside path
Try with this . Hope this helps
<td><img src="{{ asset('images/'.$item->image) }}" alr="image"></td>
Although it cant be the definite solution because it really depends upon how you have saved your image and its better if u have posted the controller logic too .
Let me know !
Change your this code from this
<img src="{{ asset('public/images/'.$item->image) }}
to this :
<img src="{{ asset('/images/'.$item->image) }}
Here You just Ignore the public because you use asset..In the simplest form, assets means JavaScript, CSS, and images which lie directly under the public directory and are publicly accessible using a URL.
Laravel provides a helper function, asset(), which generates a URL for your assets.
So just add write this
<td><img src="{{ asset('/images/'.$item->image) }}" alr="image"></td>
But My suggestion to you that when you save the file on database you save it with the all path after public path
how about use url()
<img src="{{url('images/'.$item->image)}}" alt="Image"/>

404 not found laravel routing

what's wrong with my routes? it can't redirect to my edit-info form inside InfosController
web.php
route::get('/info-admin/{$info}/edit','InfosController#edit');
info-admin.blade.php
#foreach ( $info as $infos )
<tr>
<th scope="row" class="align-middle">{{ $loop->iteration }}</th>
<td class="align-middle">{{ $infos->judul }}</td>
<td class="align-middle">{{ $infos->konten }}</td>
<td class="align-middle">{{ $infos->image }}</td>
<td class="align-middle">{{ $infos->created_at }}</td>
<td class="align-middle">{{ $infos->Updated_at }}</td>
<td class="align-middle form">
<button type="submit" class="btn btn-info mb-3">Edit</button>
<form method="POST" action="{{ route('infos.destroy', [$infos->id]) }}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">Hapus</button>
</form>
</td>
</tr>
#endforeach
InfosController#edit
public function edit($id)
{
return view('admin.edit-info');
}
what did I do wrong, why laravel can't find my routes?
You route is defined wrong.
route::get('/info-admin/{$info}/edit','InfosController#edit');
Should be without $ and name the route for easier linking.
route::get('/info-admin/{info}/edit','InfosController#edit')->name('infos.edit');
Instead of hard coding it, use route as you do with destroy on the edit link too.
<a href="{{ route('infos.edit', [$infos->id]) }}">
To check if you routes are defined correctly, try running in the project.
php artisan route:list
There are multiple ways by which you can accomplish this:
Edit
Edit

Multiple delete in Laravel

I'm currently trying to do multiple deletion in Laravel 5.3. I'd like to be something just like how emails are deleted using checkbox.
But before deleting, I'd like to do confirmation using a modal.
form in my blade looks like this
<form id="linkForm" action="/links/deleteLinks" method="POST">
{{ csrf_field() }}
{{ method_field('POST') }}
<div class="ibox-content">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover dataTables-example footable toggle-arrow-tiny">
<div class="btn-group btn-group-sm pull-right">
<a data-toggle="modal" class="btn btn-primary" href="#modal-form-add" data-dismiss="modal"><i class="fa fa-plus-square-o"></i> Add</a>
<input class="btn btn-danger" type="submit" value="Delete">
</div>
<thead>
<tr>
<th></th>
<th data-toggle="true" data-hide="all">Company</th>
{{-- <th>Category</th> --}}
<th>Page Url</th>
<th data-hide="all">Domain Url</th>
<th>Destination Url</th>
<th>Contact Email</th>
<th data-hide="all">Trust Flow</th>
<th data-hide="all">Estimated Traffic</th>
<th data-hide="all">Domain Authority</th>
<th>Status</th>
<th>Note</th>
<th data-hide="all">Live</th>
<th>Action</th>
{{-- <th>Delete</th> --}}
</tr>
</thead>
<tbody>
#foreach($links as $link)
<tr>
<td align="center"><input type="checkbox" class="i-checks" id="input" name="input[]" value="{{ $link->page_url }}"></td>
<td>{{ $link->company->name }}</td>
{{-- <td>{{ $link->category_id }}</td> --}}
<td>{{ $link->page_url }}</td>
<td>{{ $link->domain_url }}</td>
<td>{{ $link->destination_url }}</td>
<td>{{ $link->contact_email }}</td>
<td>{{ $link->trust_flow}}</td>
<td>{{ $link->estimated_traffic }}</td>
<td>{{ $link->domain_authority }}</td>
<td>{{ $link->status }}</td>
<td>{{ $link->comment }}</td>
<td>
#if($link->is_live)
{{ 'Live' }}
#else
{{ 'Down' }}
#endif
</td>
<td>
<a data-toggle="modal" href="#modal-form-{{ $link->id }}" data-dismiss="modal"><i class="fa fa-pencil"></i> Edit</a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</form>
modal.blade.php
<div id="myModal" class="modal inmodal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body text-center">
<i class="fa fa-exclamation-triangle modal-icon"></i>
<h2><strong>Are you sure?</strong></h2>
{{-- <p class="text-muted">Are you sure you want to delete the following link(s)?</p> --}}
<p id="checkid"></p>
<div class="row">
<button id="cancelDelete" type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button class="btn-primary btn" id="SubForm">Confirm and Submit The Form</button>
</div>
</div>
</div>
</div>
javascript
$("#linkForm").validate({
submitHandler: function (form) {
$("#myModal").modal('show');
$('#SubForm').click(function () {
form.submit();
});
}
});
How can I submit my array to the controller without creating another function and route for multiple delete and just use links.destroy?
You can pass the array from the form like this
<input type="checkbox" name="ids[]" value="{{$row->id}}"/>
In your controller you can do
Model::whereIn('id', $request->ids)->destroy();
You can always collect the links through JS and send them along. In the Laravel controller you'll simply add an if condition to differentiate between the two requests.
By the by, whether it is only one id or several, you should always make sure that permissions over them are enforced.
Elaborating on my answer,
Suppose you've got a collection of articles, each of which can be marked for deletion
<input type="checkbox" name="article[]" value="1" checked>
<input type="checkbox" name="article[]" value="2" checked>
<input type="checkbox" name="article[]" value="3" checked>
<input type="checkbox" name="article[]" value="4">
Then you can collect those ids with
let ids = Array.from(document.querySelectorAll('input[name="article[]"'))
.filter(el => el.checked)
.map(el => el.value);
And later on send them thorugh an XMLHttpRequest

laravel, checkbox wont pass form

Why won't checkbox pass into Input? With other input types (text,password,select) it works fine.
form
<form method="POST" enctype="multipart/form-data" id="form">
#foreach ($query as $article)
<tr>
<td>{{ $article->title }}</td>
<td>{{ $article->slider }}</td>
<td><i class="fa fa-edit"></i> Edit</td>
<td><input type="checkbox" class="group-checkable" name="article[]" value="{{ $article->id }}"></td>
</tr>
<input name="test" value="tedt" hidden>
#endforeach
</form>
and controller
if (Request::isMethod('post'))
{
dd(Input::get('article'));
}

Resources