I have a little problem with passing some data from my view to my controller.
Please have a look at this code snippet:
{!! Form::open(['action' => 'DomainController#detach', 'method' => 'post']) !!}
#foreach($domains as $domain)
<tr>
<td>{{ $domain->name }}</td>
<td>{{ $domain->tld }}</td>
<td id="hello">
#foreach($domain->tags as $tag)
{{ $tag->name }},<br>
#endforeach
</td>
<td>
#foreach($domain->tags as $tag)
{!! Form::hidden('tag_id[]', $tag->id) !!}
<button name="domain_id" value="{{ $domain->id }}" class="glyphicon glyphicon-trash"></button>
<br>
#endforeach
</td>
</tr>
#endforeach
{!! Form::close() !!}
In my Controller is a :
$input = Input::all();
return $input;
In my code ( in the last ) is a button. If I press teh button, I'm getting directed to my controller action. I'm returning the die data in my $input variable and it allways shows me the same tag_id. Allways the very last tag_id of this domain. I don't know why and couldn't figure it out.
It should be:
{!! Form::hidden('tag_id[]', $domain->pivot->id) !!}
you're missing the [] in input
You have 2 fields with the same name:
{!! Form::hidden('tag_id', $domain->pivot->id) !!}
<button name="tag_id" value="{{ $domain->id }}" class="glyphicon glyphicon-trash"></button>
Related
I wanted to add pagination to my project and followed a tutorial for that. When I click new page the results are not changing.
In my Controller I added this $competitions = Competition::latest()->paginate(1);
Then into app/Providers/AppServiceProvider added this use Illuminate\Pagination\Paginator; and inside the boot function added this: Paginator::useBootstrap();
And the last thing was to add this {{ $competitions->onEachSide(1)->links() }} after I close my table in the blade file.
EDIT:
Controller function:
public function index()
{
$competitions = Competition::latest()->paginate(1);
return view('competition.index', compact('competitions'));
}
Loop in blade:
#foreach ($competitions as $competition)
<tbody >
#switch($competition->status)
#case('active')
<tr>
#break
#case('to_push')
<tr class="bg-secondary">
#break
#case('inactive')
<tr class="bg-warning">
#break
#default
<tr>
#endswitch
<td>{{ $competition->id }}</td>
<td>{{ $competition->name }}</td>
<td>{{ $competition->status }}</td>
<td>{{ $competition->mode }}</td>
<td>{{ $competition->type }}</td>
<td>{{ $competition->frequency }}</td>
<td>{{ $competition->last_push }}</td>
<td>{{ $competition->next_push }}</td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('events.list_by_competition',['competition' => $competition->id]) }}">Events</a></td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('competitions.exports',$competition->id) }}">Exports</a></td>
<td class="text-center">
<form action="{{ route('competitions.destroy',$competition->id) }}" method="POST">
<a class="btn btn-primary btn-sm" href="{{ route('competitions.edit',$competition->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $competitions->links('pagination::bootstrap-5') }}
dd(Competition::all()) returns the 4 records I have
Can you please tell me what I did wrong? Any and all help gratefully received.
$competitions = Competition::latest()->paginate(1) this command will return you one result and then pagination will take 1 per page so it won't be shown at all.
Try something like this in your controller
$competitions = Competition::paginate(1)
return view(...., ['competitions' => $competitions]);
and in your blade add this under the foreach loop where you display data
{{ $competitions->links() }}
Read more here
HERE IS HOW TO MAKE PAGINATION
BLADE
#foreach($paginate as $p)
{{ $p->name }} <br>
#endforeach
{{ $paginate->links() }}
CONTROLLER
return view('backend.test', [
'paginate' => Product::paginate(5)
]);
I used the following code to display the role of the user
however, when I want to edit or delete it leads me to the deleted ids number 2/3 it return the same thing even when I change the for else with foreach Attempt to read property "id" on null (View: C:\laravel\gauto\resources\views\dashboard\user\edit.blade.php)!
when I delete the nested foreach loop everything back to normal
thank you in advance
#forelse($users as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->email }}</td>
<td>
#if($item->email_verified_at)
{{ "verified" }}
#else
{{ "not verified" }}
#endif
</td>
<td>
#foreach($item->roles as $item)
{{ $item['name'] }}
#endforeach
</td>
<td>
Edit
<form action="{{ route('user.destroy', $item->id) }}" method="POST">
#csrf
#method('delete')
<button onclick="return confirm('Are you sure ??')" type="submit">Delete</button>
</form>
</td>
</tr>
#empty
<tr>
blank data
</tr>
#endforelse
Your users are also saved in $item and in the loop you are allocating $item a different value every time.
Try changing the code to
<td>
#foreach($item->roles as $role)
{{ $role['name'] }}
#endforeach
</td>
I know it sounds stupid but I've got strange issue with deleting my subcategories, when I try to delete my sub it gives me error that name and slug is required! seems I try to add input in database!
here is some images to make it clear for you:
Here is my controller destroy function:
public function destroy($id)
{
$subcategory = Subcategory::find($id);
$subcategory->delete();
Session::flash('success', 'Your Sub-Category Deleted successfully!');
return redirect()->route('subcategories.index');
}
And this is my form which you see in images above:
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Subcategory Name</th>
<th>Slug</th>
<th>Parent Category</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
#foreach ($categories as $category)
#foreach($category->subcategories as $sub)
<tr>
<td>{{ $sub->id }}</td>
<td>{{ $sub->name }}</td>
<td>{{ $sub->slug }}</td>
<td>{{ $sub->category->name }}</td>
<td>
Edit
{!! Form::model(['route' => ['subcategories.destroy', $sub->id], 'method' => "DELETE"]) !!}
{{ Form::submit('Delete', ['class' => 'btn btn-sm btn-danger']) }}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
I think it caused the Form::open return:
<form method="DELETE" action="{{route('subcategories.destroy', ['id' => $sub->id])}}">
I think that is the problem, I have same probleme here and I have read about this in Laracast.
So in my case I change not use Form::open(). I use manual form, and my code looked like this:
<form method="POST" action="{{route('subcategories.destroy', ['id' => $sub->id])}}">
{{ method_field('DELETE') }}
.
.
.
</form>
I hope it can help you :)
This is my route
Route::delete('/customer_page/summary/{id}', 'ReservationController#delete_cart');
My controller function
public function delete_cart($id)
{
cart::findOrFail($id)->delete();
return redirect('/customer_page/summary');
}
My form
#foreach($cart as $key => $val)
<tr>
<td>{{ $val->room_type }}</td>
<td>{{ $val->number_of_rooms }}</td>
<td>{{ $val->price }}</td>
<td>
<form action="/customer_page/summary/{{ $val->id }}" method="post">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit" class="btn waves-effect red"><i class="material-icons">delete</i>delete</button>
</form>
</td>
</tr>
#endforeach
I don't know why I always keep getting that error
You need to set your method="POST" on your form, then inside of the form, use Laravel's method_field() helper: {{ method_field('DELETE') }}. Do not attempt to set your method to delete directly.
The other answer using the form helper package form laravel-collective will work fine too, but that's not included by default in Laravel anymore, so I thought it prudent to outline how to achieve this using raw HTML.
Do the following(RESTFUL)
Route::resource('reservations', 'ReservationController');
OR(NON RESTFUL)
Route::delete('delete/{id}',array('uses' => 'ReservationController#destroy', 'as' => 'My.route'));
Controller
public function destroy($id)
{
$item = Reservation::findOrFail($id);
$item->delete();
}
View
#foreach($reservations as $item)
<tr>
<td>{{ $item->description }}</td>
<td>
{{ Form::open(['method' => 'DELETE', 'route' => 'reservations.destroy', $item->id]) }}
{{ Form::hidden('id', $item->id) }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{{ Form::close() }}
</td>
</tr>
#endforeach
OR (for non restful)
{{ Form::open(['route' => ['My.route', $value->id], 'method' => 'delete']) }}
<button type="submit">Delete</button>
{{ Form::close() }}
If you are using the below
{!! Form::model($user,
['method' => 'DELETE',
'route' => ['users.destroy', $user->id],
]) !!}
{{ method_field('DELETE') }}
// then you form goes here
{{ method_field('DELETE') }}
{!! Form::close() !!}
You need to change method on form, from POST to DELETE
You see on route you have defined as DELETE:
Route::delete('/customer_page/summary/{id}', 'ReservationController#delete_cart');
I'm trying to create a form to delete a row in my db:
#foreach ($CostStatuses as $CostStatus)
<tr>
<td class="table-text">
<div>
<a href="/costStatus/{{ $CostStatus->id }}/edit">
{{ $CostStatus->status_name }}
</a>
</div>
</td>
<td>
{{ Form::open(array('url' => 'CostStatus/' . $CostStatus->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete this Nerd', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
</td>
</tr>
#endforeach
But the html shows the form inside the {{ }} as a text to the user.
That's because {{}} does not execute the contents inside. Use {!! !!}.
LAready fixed:
Just change {{ for {!!.