I wish to submit a form where there are rows in a foreach loop by ticking the checkbox. When there is a row outside of the foreach loop I seem to receive the id data into my controller however when it is outside of the foreach loop it is just skipped. Here is my code sample? What am I missing?
<form action="{{ url('/staff') }}" method="POST" role="form" id="form" class="form-horizontal">
{{ csrf_field() }}
#foreach ($items as $item)
<tbody>
<tr>
<td >
{{ $item->first_name }} {{ $item->last_name }}
</td>
<td class="center">
<div class="checkbox-table">
<label>
<input name="item[]" value="{{ $item->id }}" type="checkbox" class="flat-grey">
</label>
</div>
</td>
</tr>
#endforeach
</tbody>
<div>
<button class="btn btn-yellow" type="submit">
Save</i>
</button>
</div>
</form>
Works by removing this from the input label.
class="flat-grey"
Related
Hello how do i check if database row insert exist in laravel? i want to display something entries if > 0 and i want to display else if entries = 0. but i don t know how to do that. i tried with forelse, if else and i got same error. Undefined variable $istProj.
<div class="card-body">
#if ($istoric->isEmpty())
#forelse ($istoric as $istProj)
<div class="mb-3">
<table class='table'>
<tr class="table-row-heads">
<th>Id Proiect</th>
<th>Tip actiune </th>
<th>Colaborator </th>
<th>Suma </th>
<th>Data </th>
</tr>
<tr class="table-row-data">
<td>{{ $istProj->id_proiect }}</td>
<td>{{ $istProj->action_type }}</td>
<td>{{ $istProj->colaborator_id }}</td>
<td>{{ $istProj->suma }}</td>
<td>{{ $istProj->data }}</td>
</tr>
</table>
</div>
#empty
<div class="card-body">
<h1>Nu au fost gasite inregistrari</h1>
</div>
#endforelse
#endif
</div>
<form action="{{ url('/') }}" method="POST">
#csrf
#method('PUT')
<div class="mb-3">
<label class="form-label">Id proiect</label>
<input type="text" class='form-control' value="{{ $proiecte->id }}" name='id_proiect' id='id_proiect' placeholder="Id proiect">
</div>
<div class="mb-3">
<label class="form-label">Tip actiune</label>
<select class="form-select" aria-label="Default select example" name='Status_Tranzactii'>
<option selected>Alege tipul actiunii (0 = cheltuiala, 1 = plata, 2 = incasare)</option>
<option value="cheltuiala">0</option>
<option value="plata">1</option>
<option value="incasare">2</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Colaborator</label>
<select class="form-select" aria-label="Default select example" name="Colab_id">
<option selected>Alege colaboratorul (daca este cazul)</option>
#foreach ($colaborator as $colaboratori)
<option value="{{ $colaboratori->id }}">{{ $colaboratori->id }} </option>
#endforeach
</select>
</div>
<div class="mb-3">
<label class="form-label">Suma</label>
<input type="text" class='form-control' value="{{ $istProj->suma }}" name='suma' placeholder="Introduceti suma">
</div>
<div class="mb-3">
<label class="form-label">Data</label>
<input type="text" class='form-control' value="{{ $istProj->data }}" name='data' placeholder="Introduceti data">
</div>
<button type='submit' class='btn btn-primary' style="float: right;">Adauga</button>
</form>
How can i make that to work?
Error on line with "Suma"
If I understand well, you already have a Collection result (From Eloquent or anywhere) and you need to do different action based on that result.
I'm,right? If yes....
To check the amount or entries you can use $result->count(), so in Blade you can:
#if($result->count() > 0)
Do something
#else
Do anotherthing
#endif
<form action="/group/{id}/save/group" method="POST">
#csrf
<table id="modal-table">
#foreach($user_list->get() as $f)
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
#endforeach
</table>
</form>
Hey a small problem! in the Foreach loop and every member in the list, I tried some bare-bones to get each users id and hobby id (to add to database, but that's not important). For some reason, when I take the input 'name' and click the Go button, All "id's" of everyone in the list are executed, Not the individual id of each user. Meaning.. each user displays their individual id and a go button, but click go and all id's are collected, not just one. It's probably really obvious but any ideas why this is happening?? Thanks!
Like said Tim Lewis in the comment, you need to put your form inside your foreach, so when the "Go" button is clicked, only the corresponding form will send the data, like this :
<table id="modal-table">
#foreach($user_list->get() as $f)
<form action="/group/{id}/save/group" method="POST">
#csrf
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
</form>
#endforeach
</table>
<table id="modal-table">
#foreach($user_list->get() as $f)
<form action="/group/{id}/save/group" method="POST">
#csrf
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
</form>
#endforeach
</table>
check this part of Jérôme W post
<form action="/group/{id}/save/group" method="POST">
Change to this
<form action="/group/{{$group->id}}/save/group" method="POST">
I'm using the HTML form, not Laravel Collective.
For now I've successfully created a CRUD for a users in my CMS, but one thing bothers me:
How can I set a Delete button in my list of users, instead of the specific edit page?
Also, it will be nice when a user clicks on the Delete button to show up confirmation popup for deleting the specific user.
So, here's my code:
The controller:
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
return redirect('/admin/users');
}
The list of users page:
#extends('layouts.backend')
#section('content')
<h1>Users</h1>
<a class="btn btn-primary" href="/admin/users/create">Create new user</a>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
#if($users)
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role ? $user->role->name : 'User has no role'}}</td>
<td>{{$user->status == 1 ? 'Active' : 'Not active'}}</td>
<td>{{$user->created_at->diffForHumans()}}</td>
<td>{{$user->updated_at->diffForHumans()}}</td>
<td>
Edit
<a class="btn btn-danger" href="">Delete</a> // HOW TO ACHIEVE THIS?
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
#endsection
The specific edit user page:
#extends('layouts.backend')
#section('content')
<h1>Edit user</h1>
<form method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-control" value="{{$user->name}}">
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" name="email" class="form-control" value="{{$user->email}}">
</div>
<div class="form-group">
<label>Role:</label>
<select name="role_id" class="form-control">
#if($user->role_id == 1)
<option value="1" selected>Administrator</option>
<option value="2">Editor</option>
#else
<option value="1">Administrator</option>
<option value="2" selected>Editor</option>
#endif
</select>
</div>
<div class="form-group">
<label>Status:</label>
<select name="status" class="form-control">
#if($user->status == 1)
<option value="1" selected>Active</option>
<option value="0">Not active</option>
#else
<option value="1">Active</option>
<option value="0" selected>Not active</option>
#endif
</select>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" value="{{$user->password}}">
</div>
<div class="form-group">
<input type="submit" name="submit" value="Update user" class="btn btn-primary">
</div>
</form>
<form id="delete-form" method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<input type="submit" class="btn btn-danger" value="Delete user">
</div>
</form>
#include('inc.errors')
#endsection
The route:
Route::group(['middleware'=>'admin'], function(){
Route::resource('admin/users', 'AdminUsersController');
Route::get('/admin', function(){
return view('admin.index');
});
// Route::resource('admin/posts', 'AdminPostsController');
});
It's not obvious from the code you posted, but your DELETE route expects DELETE method. As it should!
But on your list you're trying to access it with GET method.
Really you should just reuse the code from the edit page, which fakes DELETE method already.
Something like this:
...
<td>
Edit
<form method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<input type="submit" class="btn btn-danger delete-user" value="Delete user">
</div>
</form>
</td>
...
...
// Mayank Pandeyz's solution for confirmation customized for this implementation
<script>
$('.delete-user').click(function(e){
e.preventDefault() // Don't post the form, unless confirmed
if (confirm('Are you sure?')) {
// Post the form
$(e.target).closest('form').submit() // Post the surrounding form
}
});
</script>
As you have stated it will be nice when a user clicks on the Delete button to show up confirmation popup for deleting the specific user. For this you have to use jquery and ajax. Change the following code:
<a class="btn btn-danger" href="">Delete</a>
to
<a class="btn btn-danger delete_user" href="javascript:void(0);" id="{{$user->id}}">Delete</a>
and put an event listener like:
$('.delete_user').click(function(){
if( confirm('Are you sure?') )
{
var id = $(this).attr('id');
// Make an ajax call to delete the record and pass the id to identify the record
}
});
You can update your code like:
<a class="btn btn-danger" href="/admin/users/{{$user->id}}/delete" >Delete</a>
OR you should delete user using route name like:
Delete
option with "laravel form helper" and jquery
<div class="actions">
<a href="#" class="list-icons-item delete-action">
<i class="icon-trash"></i>
</a>
{{ Form::open(['url' => route('admin.users.destroy', $user), 'method' => 'delete']) }}
{{ Form::close() }}
</div>
<script>
$(document).ready(function () {
$('.delete-action').click(function (e) {
if (confirm('Are you sure?')) {
$(this).siblings('form').submit();
}
return false;
});
});
</script>
Using route closures to delete
show.blade.php
<form>
<h1>Title: {{$tutorial->title}}</h1>
<p>Title Description: {{$tutorial->title_description}}</p>
<p>Video: {{$tutorial->video}}</p>
<form action="{{ route('delete-tutorial', [$tutorial->id])}}"
method="post">
#csrf
#method('DELETE')
<button class="btn btn-primary" onclick="return confirm('Are you sure?')"
type="submit" name="Delete">Delete</button>
</form>
for the route for deleting
Route::delete('tutorial/{id}',function($id){
$tutorial = Tutorial::findOrFail($id)->first();
$tutorial->delete();
return redirect('tutorial');
})->name('delete-tutorial');
Also don't forget to add this on your routes/web.php
use App\Models\Tutorial;
I'm working on a Laravel (v 5.4) project and i did the CRUD to manage categories. Currently, i can create a new category and i would be able to delete.
I created the view (with blade) to delete the categories :
<table class="table">
<thead>
<th>Name</th>
<th>Action</th>
</thead>
<tbody>
#foreach ($categories as $category)
<tr>
<td>$category->name</td>
<td>
<a href="{{ url('/categories', ['id' => $category->id]) }}">
<button class="btn btn-default">
Delete
</button>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
And in the routing file web.php, i wrote :
Route::delete('/categories/{id}', CategoryController#destroy);
I have a controller CategoryController with a method destroy() who delete category and redirect to list of categories. But when i click on the button to delete, i get an error that explain this route is not define. If i replace Route::delete with Route::get it works. I think the url is called with GET but i would keep that for an other action.
I tried to replace the link with a form and "DELETE" as the value of "method" attribute but it didn't work.
How can i call url with DELETE method to catch it with Route::delete ?
Thanks in advance.
If you click on an url it will always be a GET method.
Since you wish to define it as DELETE, you should remake it into a post form and add
<input type="hidden" name="_method" value="delete" />
in it. Like replace:
<a href="{{ url('/categories', ['id' => $category->id]) }}">
<button class="btn btn-default">Delete</button>
</a>
with:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
<input type="hidden" name="_method" value="delete" />
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Same goes for PUT request.
Since Laravel 5.1 method_field:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
{!! method_field('delete') !!}
{!! csrf_field() !!}
</form>
Since Laravel 5.6 just with # tag:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
#method('delete')
#csrf
</form>
For laravel 5.7 please look my example:
<form action="{{route('statuses.destroy',[$order_status->id_order_status])}}" method="POST">
#method('DELETE')
#csrf
<button type="submit">Delete</button>
</form>
Any method other than GET and POST requires you to specify the method type using a hidden form input. That's how laravel detects them. In your case you need to send the delete action using a form. Do this.
<table class="table">
<thead>
<th>Name</th>
<th>Action</th>
</thead>
<tbody>
#foreach ($categories as $category)
<tr>
<td>$category->name</td>
<td>
<form action="/categories/{{ $category->id }}" method="post">
{{ method_field('delete') }}
<button class="btn btn-default" type="submit">Delete</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
I am inserting a reduced trip price for my listing. In my form, I display all the trips for that listing. I don't know how to insert the already existing Trip name into the database though. I will show you what i mean.
( I'm not editing a trip, I'am creating a Promotional Listing, so the trip name and reduced price will go into a diffrent table )
I need to insert the "Trip" name on the left
This is how I have the form laid out:
<form class="form" method="post" action="{{ route('user.promotion-store') }}">
{{ csrf_field() }}
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Trip</th>
<th>Price</th>
<th>Reduced Price</th>
</tr>
</thead>
<tbody>
#foreach( $listings->trips as $trip )
<tr>
<td>
{{ $trip->name }}
</td>
<td>
${{ $trip->cost }}
</td>
<td>
<div class="col-md-12">
<div class="form-group{{ $errors->has('reduced_trip_price') ? ' has-error' : '' }}">
<label>Your Reduced Price</label>
<input type="text" class="form-control" name="reduced_trip_price" id="reduced_trip_price" placeholder="Optional...">
#if($errors->has('reduced_trip_price'))
<span class="help-block">{{ $errors->first('reduced_trip_price') }}</span>
#endif
</div>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
</form>
And this is how I insert it:
public function store(ReducedTripRequest $request) {
$reducedTrips = ReducedTrips($request->all());
$reducedTrips->save();
return redirect()->back();
}
Pass the Trip name within the form in a hidden input field this way:
<input type="hidden" name="trip_name" value="{{ $trip->name }}">
Then on the server side you can get the trip_name and reduced_trip_price to do whatever with them as you want.
To insert in a table for example:
<?php
NewTable::create([
'trip_name' => $request->input('trip_name'),
'reduced_trip_price' => $request->input('reduced_trip_price'),
]);