n+1

public function index(Request $request) { $search_query = $request->get('search'); $nebulizations = Nebulization::query(); if ($search_query){ $nebulizations->search($search_query); } $nebulizations = $nebulizations->with(['patient','nurse','medicineNebulizations'])->latest()->paginate(10)->withQueryString(); return view('nebulizations.index', compact('nebulizations')); } this gives me 10queries, and it takes 1s to load a single record. the medicine relations are coming from MedicineNebulization
6 Replies
tuto1902
tuto190215mo ago
can you share the @foreach on the nebulizations/index.blade.php template?
shaan
shaanOP15mo ago
Now the problem solved. But got more.
```php
<tbody class="pb-8 pl-4">
<tr>
<td class="pl-4 w-[45%] py-2">
<select id="medicine"
name="medicine[]"
class="font-thin text-sm w-full p-2 border border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm">
<option value="" selected disabled>Select a medicine</option>

@foreach( $medicines as $medicine )
<option
value="{{ $medicine->id }}" {{ old("medicine.0") == $medicine->id ? 'selected' : '' }}>{{ $medicine->name }}</option>
@endforeach
</select>
</td>
<td class="pl-4 w-[45%] py-2">
<input type="text" name="amount[]" placeholder="eg: 5m l, 3 gm, 5 ltr"
value="{{ old("amount.0") }}"
class="font-thin text-sm w-full p-2 border border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm">
</td>
<td class="pl-4">
</td>
</tr>

</tbody>
```php
<tbody class="pb-8 pl-4">
<tr>
<td class="pl-4 w-[45%] py-2">
<select id="medicine"
name="medicine[]"
class="font-thin text-sm w-full p-2 border border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm">
<option value="" selected disabled>Select a medicine</option>

@foreach( $medicines as $medicine )
<option
value="{{ $medicine->id }}" {{ old("medicine.0") == $medicine->id ? 'selected' : '' }}>{{ $medicine->name }}</option>
@endforeach
</select>
</td>
<td class="pl-4 w-[45%] py-2">
<input type="text" name="amount[]" placeholder="eg: 5m l, 3 gm, 5 ltr"
value="{{ old("amount.0") }}"
class="font-thin text-sm w-full p-2 border border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm">
</td>
<td class="pl-4">
</td>
</tr>

</tbody>
var tr = `<tr class="pb-4">
<td class="pl-4 w-[45%] py-2">
<select id="medicine"
name="medicine[]"
class="font-thin text-sm w-full p-2 border border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm">
<option value="[]" selected disabled>Select a medicine</option>
@foreach( $medicines as $medicine )
<option value="{{ $medicine->id }}" {{ old("medicine.0") == $medicine->id ? 'selected' : '' }}> {{ $medicine->name }}</option>
@endforeach
</select>
</td>
<td class="pl-4 w-[45%] py-2">
<input type="text" name="amount[]" placeholder="eg: 5m l, 3 gm, 5 ltr" value="{{ old("amount.0") }}" class="font-thin text-sm w-full p-2 border rounded border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"></td>
<td>
<a href="javascript:void(0)" id="deleteRow"
class="">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-circle-minus text-rose-600" width="25" height="25" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0"></path>
<path d="M9 12l6 0"></path>
</svg>
</a>
</td>
</tr>`;
var tr = `<tr class="pb-4">
<td class="pl-4 w-[45%] py-2">
<select id="medicine"
name="medicine[]"
class="font-thin text-sm w-full p-2 border border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm">
<option value="[]" selected disabled>Select a medicine</option>
@foreach( $medicines as $medicine )
<option value="{{ $medicine->id }}" {{ old("medicine.0") == $medicine->id ? 'selected' : '' }}> {{ $medicine->name }}</option>
@endforeach
</select>
</td>
<td class="pl-4 w-[45%] py-2">
<input type="text" name="amount[]" placeholder="eg: 5m l, 3 gm, 5 ltr" value="{{ old("amount.0") }}" class="font-thin text-sm w-full p-2 border rounded border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"></td>
<td>
<a href="javascript:void(0)" id="deleteRow"
class="">
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-circle-minus text-rose-600" width="25" height="25" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0"></path>
<path d="M9 12l6 0"></path>
</svg>
</a>
</td>
</tr>`;
, how to load the old values from a dynamic input fields? here is the code.
tuto1902
tuto190215mo ago
I think I already replied to this one but again, you can use {{ old('field_name') }} to render the old value from a field. Is old('amount.0') not loading the value?
shaan
shaanOP15mo ago
Not working @foreach($nebulization->medicineNebulizations as $medicine_used) <tr> <td class="pl-4 w-[45%] py-2"> <select id="medicine" name="medicine[]" class="font-thin text-sm w-full p-2 border border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"> @foreach( $medicines as $medicine ) <option value="{{ $medicine->id }}" @selected($medicine_used->id == $medicine->id)>{{ $medicine->name }}</option> @endforeach </select> </td> <td class="pl-4 w-[45%] py-2"> <input type="text" name="amount[]" value="{{ $medicine_used->amount }}" placeholder="eg: 5m l, 3 gm, 5 ltr" class="font-thin text-sm w-full p-2 border border-gray-200 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm"> </td> This is the edit form Its loading the amount correct But the medicine
shaan
shaanOP15mo ago
No description
tuto1902
tuto190215mo ago
Can you share the definition of the Nebulization model class? I want to take a peek into the relationship names. Also, you can use three backticks (`) and the language name to share code with proper highlight, like this
function myCode() {}
function myCode() {}
No description
Want results from more Discord servers?
Add your server