Laravel 5 Fetch the right id from table - php

I have a html table with a list of cars from my database, I wish to select a row in the table to open a bootstrap modal with the right id from the db table. The button "Change Status" opens the modal showing the car, numberplate and status. But it only fetches the latest id. When I move the modal to the top, It fetches the first id on the table. I'm baffled on what I can do to fix this.
Here's my html code:
#if (isset($results))
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th width="15%"></th>
<th>#</th>
<th>Numberplate</th>
<th>Status</th>
<th>Added</th>
<th>Changed</th>
<th>Change</th>
</tr>
</thead>
#foreach ($results as $result)
<tbody>
<tr>
<td></td>
<td>{{ $result->id }}</td>
<td>{{ $result->LicencePlate }}</td>
<td>{{ $result->Status }}</td>
<td>{{ $result->created_at }}</td>
<td>{{ $result->updated_at }}</td>
<td>
<button type="button" class="btn btn-secondary btn-sm" style="background-color: #000; color: #FFF;" data-toggle="modal" data-target="#myModal">Change Status</button>
</td>
</tr>
</tbody>
#endforeach
#endif
</table>
</div>
#if (isset($cars))
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th width="15%"></th>
<th>#</th>
<th>Numberplate</th>
<th>Status</th>
<th>Added</th>
<th>Changed</th>
<th>Change</th>
</tr>
</thead>
#foreach ($cars as $car)
<tbody>
<tr>
<td></td>
<td>{{ $car->id }}</td>
<td>{{ $car->LicencePlate }}</td>
<td>{{ $car->Status }}</td>
<td>{{ $car->created_at }}</td>
<td>{{ $car->updated_at }}</td>
<td>
<button type="button" class="btn btn-secondary btn-sm" style="background-color: #000; color: #FFF;" data-toggle="modal" data-target="#myModal">Change Status</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endif
</div>
</div>
</div>
#if (isset($cars))
#foreach ($cars as $car)
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<p>ID: {{ $car->id }}</p>
<p>Numberplate: {{ $car->LicencePlate }}</p>
<p>Status: {{ $car->Status }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
#endforeach
#endif
Not completely sure if a special method in my controller or you need to see my model. If so I can edit this question.

Now in your case the modal id #myModal is repeated inside the loop. Change the modal id and button id like,
<button type="button" class="btn btn-secondary btn-sm" style="background-color: #000; color: #FFF;" data-toggle="modal" data-target="#myModal_{{ $car->id }}">Change Status</button>
And modal
<div class="modal fade" id="myModal_{{ $car->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<!--Modal content here -->
<div>
FYI: Use the single loop for both button and modal.

Related

Delete button not work bootstrap modal and Laravel

I created one table that list items, and I added two buttons(edit and delete) so, the button edit is working, but the button delete is not working, and I'm making me carzy because I don't find the error. this is the table:
<table id="datatable" class="table align-items-center table-flush">
<thead class="thead-light">
<tr>
<th>text</th>
<th class="disabled-sorting" width="10%"></th>
<th class="disabled-sorting" width="10%"></th>
</tr>
</thead>
<tbody>
#foreach($Items as $item)
<tr>
<td>{{ $item->text }}</td>
<td><a href="{{ route('ItemsController.edit', $item->id) }}" class="btn btn-primary btn-fab btn-icon btn-round" title="Edit">
<i class="fa fa-edit"></i></a>
</td>
<td>
<a href="#" class="btn btn-primary btn-fab btn-icon btn-round btn-delete" title="Delete" data-id="{{ $item->id }}" data-toggle="modal" data-target="#modal-default" data-route="{{ route('ItemsController.destroy', $item->id) }}" data-title="{{ $item->id }}">
<i class="fa fa-trash"></i></a>
</td>
</tr>
#endforeach
</tbody>
</table>
the modal is:
<div class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Item</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Delete?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<form class="" action="" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger" name="button">yes, delete</button>
</form>
</div>
</div>
</div>
</div>
javascript data:
<script>
$(document).on('click', '.btn-delete', function(){
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
})
</script>
and controller function destroy
public function destroy(Items $item) {
$item->delete();
return redirect()->route('items.index')->with('success', 'Deleted Success');
}
I checked it line by line, and I don't know what I'm wrong, please help me
Many times thanks for getting into this.
You need to trigger your modal from JS.
If it is bootstrap 4 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
$("#modal-default").modal();
If it is bootstrap 5 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
modal = new bootstrap.Modal(document.getElementById("modal-default"));
modal.show();

Popup box and content on each table row

Original code
<tbody id="table">
#foreach($services as $service)
<tr>
<td colspan="7" class="theme-bg" style="color:white">{{ $service->name }}
#if($service->brand != '')
<span style="font-family:'Font Awesome 5 Brands','FontAwesome';">&#x{{ $service->brand }};</span>
#endif
</td>
</tr>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
#foreach($packages as $package)
#if(isset($categories[$service->id]) && in_array($package->category_id,explode(',',$categories[$service->id])))
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>
#php
$price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_format(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}
</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td style="white-space: pre-line">{{ $package->description }}</td>
</tr>
#endif
#endforeach
#endforeach
</tbody>
I was trying to replace "{{ $package->description }}" with a button to show the content in a new popup, using this.
<tbody id="table">
#foreach($services as $service)
<tr>
<td colspan="7" class="theme-bg" style="color:white">{{ $service->name }}
#if($service->brand != '')
<span style="font-family:'Font Awesome 5 Brands','FontAwesome';">&#x{{ $service->brand }};</span>
#endif
</td>
</tr>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
#foreach($packages as $package)
#if(isset($categories[$service->id]) && in_array($package->category_id,explode(',',$categories[$service->id])))
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>
#php
$price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_format(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}
</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">+ Description</button></td>
</tr>
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="white-space: pre-line">{{ $package->description }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
#endforeach
</tbody>
But I'm getting same content on each button, but the content must be different because each row have won service name + description. This description must be different on the popup box.
The problem might be caused by your modal-ids. Since all your modals have the id "exampleModalCenter" your Button probably only opens the first one it can find (since ids are supposed to be unique).
If you would dynamicly change your modal-ID and the corresponding Button-target
Example with adding your package-ID:
<tbody id="table">
#foreach($services as $service)
<tr>
<td colspan="7" class="theme-bg" style="color:white">{{ $service->name }}
#if($service->brand != '')
<span style="font-family:'Font Awesome 5 Brands','FontAwesome';">&#x{{ $service->brand }};</span>
#endif
</td>
</tr>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
#foreach($packages as $package)
#if(isset($categories[$service->id]) && in_array($package->category_id,explode(',',$categories[$service->id])))
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>
#php
$price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_format(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}
</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter-{{ $package->id }}">+ Description</button></td>
</tr>
<div class="modal fade" id="exampleModalCenter-{{ $package->id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="white-space: pre-line">{{ $package->description }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endif
#endforeach
#endforeach
</tbody>

Missing required parameters for [Route: admin.destroy] on Laravel form action

I have this error:
Missing required parameters for [Route: admin.destroy] [URI: admin/{admin}]
That is the all view, and all variables,
i tried a lot but i don't know what's wrong if i change put the second parameter $info
this error appears
The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
<div class="table-responsive">
<table class=" table ">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Code</th>
<th scope="col">Phone</th>
<th scope="col">Phone 2</th>
<th scope="col">Delete</th>
{{-- <th scope="col">email</th> --}}
</tr>
</thead>
<tbody>
#foreach ($infos as $info)
<tr>
<td>{{ $info->id }}</td>
<td>{{ $info->name}}</td>
<td>{{ $info->code }}</td>
<td>{{ $info->phone }}</td>
<td>{{ $info->phone2 }}</td>
<td>
<button class="btn btn-danger btn-sm" onclick="handleDelete ({{ $info->id }})">Delete
</button>
</td>
{{-- <td>{{ $info->email }}</td> --}}
</tr>
#endforeach
</tbody>
</table>
</div>
<form action="{{ route('admin.destroy',['admin' => $info])}}" method="post" id="deleteInfoForm">
#method('DELETE')
#csrf
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModal"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModal">Delete Info</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p class=" text-center text-bold">Are your sure ?</p>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No , Go back</button>
<button type="submit" class="btn btn-danger">Yes , Delete</button>
</div>
</div>
</div>
</div>
</form>
this is my delete function from AdminController
public function destroy(Info $admin)
{
// $info = Info::find($id);
$admin->delete();
// session()->flash('succuss', 'Info deleted successfully');
return redirect('/admin');
}
my routing list
| DELETE | admin/{admin} | admin.destroy | App\Http\Controllers\AdminController#destroy
The route expects parameter 2 to be the model of the id used for route model binding
Add it to the action in the form
<form action="{{ route('admin.destroy', ['admin' => $info]) }}"
Update
The form is outside the foreach loop and therefor $info is undefined
Pass the form inside the foreach instead
<div class="table-responsive">
<table class=" table ">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Code</th>
<th scope="col">Phone</th>
<th scope="col">Phone 2</th>
<th scope="col">Delete</th>
{{-- <th scope="col">email</th> --}}
</tr>
</thead>
<tbody>
#foreach ($infos as $info)
<tr>
<td>{{ $info->id }}</td>
<td>{{ $info->name}}</td>
<td>{{ $info->code }}</td>
<td>{{ $info->phone }}</td>
<td>{{ $info->phone2 }}</td>
<td>
<button class="btn btn-danger btn-sm" onclick="handleDelete ({{ $info->id }})">Delete
</button>
</td>
{{-- <td>{{ $info->email }}</td> --}}
</tr>
<tr>
<form action="{{ route('admin.destroy',['admin' => $info])}}" method="post" id="deleteInfoForm">
#method('DELETE')
#csrf
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModal"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModal">Delete Info</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p class=" text-center text-bold">Are your sure ?</p>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No , Go
back</button>
<button type="submit" class="btn btn-danger">Yes , Delete</button>
</div>
</div>
</div>
</div>
</form>
</tr>
#endforeach
</tbody>
</table>
</div>
Hope this helps
You're missing the data you want to delete in your form opening:
<form action="{{ route('admin.destroy', ['admin'=>$admin])}}" method="post" id="deleteInfoForm">
Before you do that, you have to pass the $admin variable to that view, in order to use it.
You're getting that error because your route expects to get an admin variable (admin/{admin}), but it isn't there when you call route in {{ route('admin.destroy')}}. You should provide it as the second parameter of the route method, in the keyed array format.

Unable to pass php variable to html using laravel

The following is my controller Wordings_test.php code
The issue is I am not able to print #wording.product. Even though, I can print #wording.template_name.
Thanks
class Wordings_test extends Controller{
function show($f3)
{
$wordings = Wordings::all()->orderBy('id')->select()->toArray();
foreach ($wordings as $wording) {
$wording['product'] = ProductTypes::where(['id' =>
$wording['product_type_id']])->select()->getAttribute('name');
}
$f3->set('wordings',$wordings);
$this->render('admin/wordings/view')
}
The following is the template code view.html
<i class="fa fa-user-plus"></i> Add New Brokerage Head Group
<div class="row">
<div class="form-group">
<div class="tab-content">
<div id="brokerageheadlist" class="active in tab-pane fade">
<table class="table table-bordered adminTable" id="brokerageheadList">
<thead>
<tr>
<th>Wording</th>
<th>Product Type</th>
<th style="width:140px;">Controls</th>
</tr>
</thead>
<tbody>
<repeat group="{{ #wordings }}" value="{{ #wording }}">
<tr>
<td>{{ #wording.template_name }}</td>
<td>
{{ #wording.product }}
</td>
<td>
<a class="btn btn-sm btn-info showLoader" href="/admin/wordings/edit-wordings/{{ #wording.id }}">Edit</a>
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal{{#brokeragehead.id}}">Delete</button>
<div id="myModal{{#brokeragehead.id}}" class="modal fade" role="dialog">
<div class="modal-dialog">

Content broken bootstrap v3.3.7 with laravel v5.4

When refresh the page then my content broken, I couldn't understand why this happening. Some time's my whole page broken. Please help me to solve this problem.
Bootstrap v: 3.3.7
laravel v: 5.4
Here is my code:
#extends('layouts.admin-user.main')
#section('main_content')
<div class="ptb-80">
<div class="users-mang">
<div class="container">
<div class="row">
<div class="col-md-12">
#include('layouts.success')
#include('layouts.errors')
<h1>User Management</h1>
<div class="portlet-title">
<div class="caption font-dark">
#if(isset($searchData))
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user-list') }}#endif"
class="btn btn-info">Back to User List</a>
#else
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user') }}#endif"
class="btn btn-success">Add User</a>
Import User
#endif
</div>
<div class="tools"> </div>
</div>
<table class="table table-striped table-bordered table-hover" id="sample_1">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Group</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if(isset($allData) && !$allData->isEmpty())
<?php $i = 0; ?>
#foreach($allData as $data)
<?php $i++; ?>
<tr>
<td>{{ $i }}</td>
<td>{{ $data->name }}</td>
<td>{{ $data->email }}</td>
<td>{{ $data->phone }}</td>
<td>{{ $data->group_id }}</td>
<td>
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user/contact/'.$data->id) }}#endif"
class="btn btn-info">Contact</a>
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user/edit/'.$data->id) }}#endif"
class="btn btn-primary">Edit</a>
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user/delete/'.$data->id) }}#endif"
onclick="return confirm('Are you sure?')"
class="btn btn-danger">Delete</a>
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="6" class="text-center"> No available data.</td>
</tr>
#endif
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endsection
Here is a broken content screenshot:
Thanks so much.

Categories