laravel make dynamic modal component
xxxxxxxxxx
//can use laravel component and pass the id after that use DB qury to qury the results
//example if you want to use it in table
//VIEW
<table id="myTable" class="display">
<thead>
<tr>
<th>No.</th>
<th>Name</th>
<th>Email</th>
<th>Remeber Token</th>
<th>action</th>
</tr>
</thead>
<tbody>
@foreach ($users as $i => $user)
<tr>
<td>{{ $i+1 }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->remember_token }}</td>
<td><button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal{{$user->id}}">
Launch demo modal
</button>
</td>
</tr>
//we pass the id
<x-modal id='{{$user->id}}' />
@endforeach
</tbody>
</table>
//COMPONENT
<div class="modal fade" id="exampleModal{{$id}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
test123
@php
//$id is the variable that we pass just now
$user = DB::table('users')->where('id',$id)->first();
@endphp
<h1>{{$id}}</h1>
<h2>{{$user->name}}</h2>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
//make sure to add parameter at constructor of your components
https://laravel.com/docs/10.x/blade#passing-data-to-components