How to pass data from a parent blade to a included modal? - php

I am trying to call a modal on a button click and pass through an $incident array, which will then be passed through the properties of a Vue component. How do you pass through data from a parent blade through to an included modal? i tried using data-attach but i think that is wrong.
I currently have a table looping through an incidents array passed through to my blade:
#foreach($incidents as $incident)
<tr>
<td>{{ $incident->service_now_incident_number }}</td>
<td>{{ $incident->business_service_reference ?: 'N/A' }}</td>
<td>{{ $incident->status }}</td>
<td>{{ $incident->raised_date->format('d/m/y') }}</td>
<td>{{ $incident->email }}</td>
<td>
<a class="btn btn-primary" data-toggle="modal" data-target="#incident-details-modal" data-attach="{{$incident}}">View</a>
</td>
</tr>
#endforeach
This is the modal i am trying to pass data to:
<div class="modal fade" id="incident-details-modal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<incident-details :incident="{{$incident}}"></incident-details>
</div>
</div>
</div>
is there a way i can pass through array data on modal load?

You can use $emit method for this purpose.
v-on directive captures the child components events that is emitted by $emit
Child component triggers clicked event:
export default {
methods: {
onClickButton (event) {
this.$emit('clicked', 'someValue')
}
}
}
Parent component receive clicked event:
<div>
<child #clicked="onClickChild"></child>
</div>
export default {
methods: {
onClickChild (value) {
console.log(value) // someValue
}
}
}

Related

laravel 9 error 404 page not found when update

At the most basic understanding, I've been trying to match the route and the form action. I think that I am doing it right but I wonder why the error keeps on showing. I may have missed something anywhere but I just really couldn't find it. Please help. With a very tight schedule, I need to complete this project.
When I submit the form it goes to this address.
index.blade.php
<tbody>
#foreach($payments as $payment)
<tr>
<td>{{ $payment->order->first_last_name }}</td>
<td>{{ $payment->order->business_name }}</td>
<td>{{ $payment->order->business_type }}</td>
<td>{{ $payment->order->phone_number }}</td>
<td>{{ $payment->order->postal_code }}</td>
<td>{{ $payment->order->email }}</td>
<td>{{ $payment->order->address }}</td>
<button type="button" class="btn btn-success btn-sm" data-action="{{ route('payments.send', $payment->id) }}" data-bs-toggle="modal" data-bs-target="#send" data-id="{{ $payment->id }}">اSend</button>
</td>
<td>
#php $status = $payment->status #endphp
<span class="{{ $status['bs_class'] }}">
{{ $status['label'] }}
</span>
</td>
<td>{{ $payment->accounting_code }}</td>
</tr>
#include('Admin.layouts.modal')
#endforeach
</tbody>
js
<script>
$('#send').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var action = button.data('action');
var orderId = button.data('id');
var modal = $(this);
modal.find('form').attr('action', action);
document.getElementById("orderId").value = orderId;
});
</script>
modal.blade.php
<div class="modal fade" id="send" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="send" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{ route('payments.accounting', $payment->id) }}" method="post">
#csrf
#method('PUT')
<input type="hidden" id="orderId" value="">
<div class="modal-header">
<h5 class="modal-title" id="sendTitle">send</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="accounting_code" class="form-label">کد accounting_code</label>
<input type="text" class="form-control" id="accounting_code" name="accounting_code">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">save</button>
</div>
</form>
</div>
</div>
</div>
web.php
Route::put('/payments/accounting/{payment}', [App\Http\Controllers\Admin\PaymentController::class,'accounting'])->name('payments.accounting');
PaymentController.php
public function accounting(Payment $payment, Request $request)
{
dd('hello');
$data = [
'accounting_code' => $request->post('accounting_code')
];
$payment->update($data);
return back();
}
Because your js code modal.find('form').attr('action', action) replaced the form action when showing the modal. Try to change the data-action from route payments.send to payments.accounting for the send button:
<button type="button" class="btn btn-success btn-sm" data-action="{{ route('payments.accounting', $payment->id) }}" data-bs-toggle="modal" data-bs-target="#send" data-id="{{ $payment->id }}">اSend</button>
Did you try it without naming the route?
delete this:
->name('payments.accounting')
and then in HTML form:
action="/payments/accounting"
I think you are mixing POST, PUT and GET Request here. Make sure you send your form with the correct method.

larave livewire with bootstrap modal

I want to send collection from livewire component to livewire blade.
This is my code:
Button in livewire blade
<button type="button" wire:click = "getShipmentHistory" class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#exampleModal">
<i class="fa fa-history"></i> #lang('orders.shipment_history')
</button>
My livewire component
public $shipment_history ;
public function getShipmentHistory()
{
$this->shipment_history = OrderShippmentHistory::with(['creator:id,name', 'reason'])->where('shippment_id', $this->shipment_id)->get();
}
When I try dd for $shipment_history, I get my collection; but when I try to access it, I get this error:
Uncaught TypeError: this._config is undefined from consolo
I want to receive collection in modal bootstrap to loop on it for showing data.
foreach in my blade
#foreach($shipment_history as $history)
<tr>
<td>{{ $history->creator->name??'-' }}</td>
<td>{{ $history->comment??t('no_comment') }}</td>
</tr>
#endforeach

Getting ID from the table blade.php to other blade.php error Symfony\Component\Routing\Exception\RouteNotFoundException

I have a table view in which I have a view button and redirect to another page carrying the id of the row that has been clicked.
<tbody>
<tr>
<?php $hold=0; ?>
#if(!empty($users))
#foreach($users as $user)
#if($user->role == 3)
<?php $hold++; ?>
<td class="py-1">{{ $user->id }}</td>
<td>{{ $user->name }} </td>
<td>{{ $user->Zone }} </td>
<td>{{ $user->Purok }}</td>
<td> Wala pa sa database </td>
<td>
<div class="template-demo">
<button type="button" onclick="location.href=' {{ route ('SupAd.View_PL_Accnt/'.$user->id) }}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>
</div>
</td>
</tr>
#endif
#endforeach
<?php if($hold == 0){
echo "<p><font color=red size='4pt'>No purok leader can be found</font></p>";
}
?>
#endif
I have here my code on web.php route
Route::group(['prefix'=>'SupAd_Purok_Leader_Account', 'middleware'=>['isSupAd', 'auth', 'PreventBackHistory']], function (){
Route::get('View_PL_Accnt', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt/{id}'); });
And I got the error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [SupAd.View_PL_Accnt/3] not defined.
You are placing the route parameter in the wrong place. Try this:
Route::get('View_PL_Accnt/{id}', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt'); });
The name is used to call a route using the route helper. You can give the url parameters to the route helper in an array. Inside your blade file use:
{{ route ('SupAd.View_PL_Accnt', [$user->id]) }}
you can change routes like
Route::get('View_PL_Accnt/{id}', [SupAdController::class, 'View_PL_Accnt'])->name('SupAd.View_PL_Accnt'); });
And change the button on click event
<button type="button" onclick="location.href='{!! route('SupAd.View_PL_Accnt', [$user->id]) !!}'" class="btn btn-outline-info btn-icon-text">
<i class="ti-search btn-icon-append"></i>View
</button>

How to change the div element after ajax call?

Only the first row of the table is changing
HTML table
<tr>
<th scope="row">{{ $loop->iteration }}</th>
<td>{{ $entrepreneur->name }}</td>
<td>{{ $entrepreneur->contact }}</td>
<td>{{ $entrepreneur->address }}</td>
<td>{{ $entrepreneur->business_name }}</td>
<td>{{ $entrepreneur->business_address }}</td>
<td>
<div id="approval">
<button class="check-btn" id="check-btn-{{ $entrepreneur->id }}" data-toggle="modal" data-target="#checkModal-{{ $entrepreneur->id }}"><i class="fa fa-check"></i></button>
<button class="delete-btn" id="delete-btn-{{ $entrepreneur->id }}" data-toggle="modal" data-target="#rejectModal-{{ $entrepreneur->id }}"><i class="fa fa-close"></i></button>
</div>
</td>
</tr>
Ajax Function
$.ajax({
type: 'post',
url: "/changeAccepted/"+entrepreneur_id,
success: function(res) {
toastr["success"]('Entrepreneur accepted succesfully');
$('#checkModal-'+entrepreneur_id).modal('hide');
$('#approval').html('Accepted');
} });
I am trying to change the two buttons on 'id=approval', right now it is changing for only the first row. I also want to change for other rows when it is accepted.
Each html element on same page has to have unique id property.
As you are using table I think you have multiple rows and each row has its own buttons placed in <div id="approval"></div> element.
When you use $('#approval') selector, Jquery gets only first one.
If you want to select all and change every element you have to use any selector but id.
The simplest way is to use .class selector.
Example & Solution:
Html:
...
<div class="approval_class">
<button class="check-btn" id="check-btn-{{ $entrepreneur->id }}" data-toggle="modal" data-target="#checkModal-{{ $entrepreneur->id }}"><i class="fa fa-check"></i></button>
<button class="delete-btn" id="delete-btn-{{ $entrepreneur->id }}" data-toggle="modal" data-target="#rejectModal-{{ $entrepreneur->id }}"><i class="fa fa-close"></i></button>
</div>
...
Javascript (Jquery): (to change all rows as accepted)
$('.approval_class').html('Accepted');
Javascript (Jquery): (to change only that row which accepted)
$('#delete-btn-' + entrepreneur_id).closest('div').html('Accepted');

pass the database value to modal popup

I want to pass the fetched database value to modal popup.I have fetched values from database and listed as table with pagination.
'name' field has link and will show model popup while triggering a link.I want to list the details of name which I check from the link in laravel 5.3.
Now model pop up trigered with 'Modal Header 3' for all link.I want to know how to pass the value modal popup and show the details of particular id. I want to know to know shall I need separate page or controller to do this.Where to write the query for modal popup.How can I display the details of specific Id.
id name age
1 xx 26
2 yy 28
3 zz 30
<table border = 1>
<tr>
<td>ID</td>
<td>Passanger Name</td>
<td>Destination</td>
<td>Created Date</td>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->p_id }}</td>
<td><a href="#" class="viewPopLink" role="button" data-id="{{ $user->p_id }}" data-toggle="modal" data-target="#myModal">{{ $user->p_name }}<a></td>
<td>{{ $user->destination }}</td>
<td>{{ $user->created_date }}</td>
</tr>
#endforeach
</table>
{{$users->links()}}
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header {{ $user->p_id }}</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
You can do it the way #Komal suggested, but I would not do another AJAX request since you already have all the data you need for the modal in your current view. This saves you also from writing another controller.
Add the data you need into the <a> element and use it to fill in the data into the modal popup by adding a simple 'on load' method for the modal.
Here is the code.
<td>
<a href="#" class="viewPopLink" role="button"
data-id="{{ $user->p_id }}" data-name="{{ $user->p_name }}" data-age="{{ $user->p_age }}"
data-toggle="modal" data-target="#myModal">{{ $user->p_name }}<a>
</td>
Add this into the modal body:
<p>
<span>ID</span><span id="modal_p_id"></span>
<span>Name</span><span id="modal_p_name></span>
<span>Age</span><span id="modal_p_age"></span>
</p>
Add this script into your view:
<script>
/* populate the modal popup when it's launched, with the data provided by the launching button .... */
$('#myModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggered the modal
var id = button.data('id'); // Extract info from data-* attributes
var age = button.data('age');
var name = button.data('name');
// Update the modal's content
var modal = $(this);
modal.find('.modal-title').text('Information for Passenger ' + name);
modal.find('#modal_p_id').text(id);
modal.find('#modal_p_name').text(filename);
modal.find('#modal_p_age').text(age);
});
</script>
Use jquery ajax request and get user data to set modal
$(document).on('click', '.viewPopLink', function() {
var user_id = $(this).data('id');
$.ajax({
url: 'user/get-details',
type: 'GET',
data: 'id='+user_id,
dataType: 'JSON',
success: function(data, textStatus, jqXHR){
var name = data.name;
$('.modal-title').html('<span>Modal Header ' + name + '</span>');
$('#myModal').modal('show');
},
error: function(jqXHR, textStatus, errorThrown){
},
});
});
Controller method
//Get user data
public function getDetails(Request $request)
{
$request_data = $request->all();
$user_id = $request_data['id'];
$user_data = User::where('id', $user_id)->first();
return response()->json($user_data);
}

Categories