What's new

PHP Laravel - (pahelp)

caparadc

Honorary Poster
Established
Joined
Oct 16, 2016
Posts
241
Solutions
1
Reaction
257
Points
187
hello po kumuha po ako ng side project thesis system po kaso dito po sa part na to yung error

ito po yung connection sa web.php

Route::controller(PatientController::class)->prefix('patients')->group(function () {
Route::get('index/{room}', 'showCurrentAdmit')->name('patients.show');
});

and then ito po yung controller

public function showCurrentAdmit($room)
{
$patients = Patient::where('room', $room)->get();

return view('patients.index', compact('patients', 'room'));
}
ito naman po yung sa html

<tbody>
@if($patient->count() > 0)
@foreach ($patient as $rs)
@if($rs->room == $room)
<tr>
<td class="align-middle">{{ $loop->iteration }}</td>
<td class="align-middle">{{ $rs-> name }}</td>
<td class="align-middle">{{ $rs-> age }}</td>
<td class="align-middle">{{ $rs-> gender }}</td>
<td class="align-middle">{{ $rs-> Birthday }}</td>
<td class="align-middle">{{ $rs-> Address }}</td>
<td class="align-middle">{{ $rs-> Contact }}</td>
<td class="align-middle">{{ $rs-> Guardian }}</td>
<td class="align-middle">{{ $rs-> room }}</td>
<td class="align-middle">
<div class="btn-group" role="group" aria-label="Basic example">
<form method="POST" action="{{ route('patients.discharge', $rs->id) }}">
@csrf
<button type="submit">Discharge</button>
</form>
</td>
</tr>
@endif
@endforeach
@endif
</tbody>

pero ang error po na lumalabas ay Undefined variable $room gusto kopo sana gawin ay may 14 rooms then every rooms po ay 1 lang ang capacity pag nalagyan na ng data yung isang room halimbawa ay room 1 yung room na to ay occupied na maraming salamat po
 
Hello!

The error you're encountering is due to the fact that the $room variable is not being passed correctly to the view. To resolve this issue, you need to make sure that you're passing the $room variable from the controller to the view. Here's what you can do:

First, you need to update your showCurrentAdmit method in the PatientController to include the room parameter in the route definition:

PHP:
public function showCurrentAdmit($room)
{
    $patients = Patient::where('room', $room)->get();

    return view('patients.index', compact('patients', 'room'));
}

Next, in your view file, you need to update the @if condition to check the patients variable instead of $patient variable. Additionally, you should change $rs to $patient in the @foreach loop. Here's the updated code:

HTML:
<tbody>
    @if($patients->count() > 0)
        @foreach ($patients as $patient)
            @if($patient->room == $room)
                <tr>
                    <td class="align-middle">{{ $loop->iteration }}</td>
                    <td class="align-middle">{{ $patient->name }}</td>
                    <td class="align-middle">{{ $patient->age }}</td>
                    <td class="align-middle">{{ $patient->gender }}</td>
                    <td class="align-middle">{{ $patient->Birthday }}</td>
                    <td class="align-middle">{{ $patient->Address }}</td>
                    <td class="align-middle">{{ $patient->Contact }}</td>
                    <td class="align-middle">{{ $patient->Guardian }}</td>
                    <td class="align-middle">{{ $patient->room }}</td>
                    <td class="align-middle">
                        <div class="btn-group" role="group" aria-label="Basic example">
                            <form method="POST" action="{{ route('patients.discharge', $patient->id) }}">
                                @csrf
                                <button type="submit">Discharge</button>
                            </form>                            
                        </div>
                    </td>
                </tr>
            @endif
        @endforeach
    @endif
</tbody>

By making these changes, the $room variable should now be available in the view, and the error should be resolved.
 
Hello!

The error you're encountering is due to the fact that the $room variable is not being passed correctly to the view. To resolve this issue, you need to make sure that you're passing the $room variable from the controller to the view. Here's what you can do:

First, you need to update your showCurrentAdmit method in the PatientController to include the room parameter in the route definition:

PHP:
public function showCurrentAdmit($room)
{
    $patients = Patient::where('room', $room)->get();

    return view('patients.index', compact('patients', 'room'));
}

Next, in your view file, you need to update the @if condition to check the patients variable instead of $patient variable. Additionally, you should change $rs to $patient in the @foreach loop. Here's the updated code:

HTML:
<tbody>
    @if($patients->count() > 0)
        @foreach ($patients as $patient)
            @if($patient->room == $room)
                <tr>
                    <td class="align-middle">{{ $loop->iteration }}</td>
                    <td class="align-middle">{{ $patient->name }}</td>
                    <td class="align-middle">{{ $patient->age }}</td>
                    <td class="align-middle">{{ $patient->gender }}</td>
                    <td class="align-middle">{{ $patient->Birthday }}</td>
                    <td class="align-middle">{{ $patient->Address }}</td>
                    <td class="align-middle">{{ $patient->Contact }}</td>
                    <td class="align-middle">{{ $patient->Guardian }}</td>
                    <td class="align-middle">{{ $patient->room }}</td>
                    <td class="align-middle">
                        <div class="btn-group" role="group" aria-label="Basic example">
                            <form method="POST" action="{{ route('patients.discharge', $patient->id) }}">
                                @csrf
                                <button type="submit">Discharge</button>
                            </form>                           
                        </div>
                    </td>
                </tr>
            @endif
        @endforeach
    @endif
</tbody>

By making these changes, the $room variable should now be available in the view, and the error should be resolved.
sinubukan kona po kaso error po din po
 
sinubukan kona po kaso error po din po
Apologies for the confusion. It seems that there is another issue in your code. The error you're encountering is likely due to a typo in your HTML code. In the <tbody> section, the check for the count of the $patients variable is incorrect.

Instead of $patients->count() > 0, it should be count($patients) > 0. Here's the corrected code:

HTML:
<tbody>
    @if(count($patients) > 0)
        @foreach ($patients as $patient)
            @if($patient->room == $room)
                <tr>
                    <td class="align-middle">{{ $loop->iteration }}</td>
                    <td class="align-middle">{{ $patient->name }}</td>
                    <td class="align-middle">{{ $patient->age }}</td>
                    <td class="align-middle">{{ $patient->gender }}</td>
                    <td class="align-middle">{{ $patient->Birthday }}</td>
                    <td class="align-middle">{{ $patient->Address }}</td>
                    <td class="align-middle">{{ $patient->Contact }}</td>
                    <td class="align-middle">{{ $patient->Guardian }}</td>
                    <td class="align-middle">{{ $patient->room }}</td>
                    <td class="align-middle">
                        <div class="btn-group" role="group" aria-label="Basic example">
                            <form method="POST" action="{{ route('patients.discharge', $patient->id) }}">
                                @csrf
                                <button type="submit">Discharge</button>
                            </form>                           
                        </div>
                    </td>
                </tr>
            @endif
        @endforeach
    @endif
</tbody>

Please update your HTML code with the correction, and it should resolve the issue. Let me know if you have any further questions!
 
Apologies for the confusion. It seems that there is another issue in your code. The error you're encountering is likely due to a typo in your HTML code. In the <tbody> section, the check for the count of the $patients variable is incorrect.

Instead of $patients->count() > 0, it should be count($patients) > 0. Here's the corrected code:

HTML:
<tbody>
    @if(count($patients) > 0)
        @foreach ($patients as $patient)
            @if($patient->room == $room)
                <tr>
                    <td class="align-middle">{{ $loop->iteration }}</td>
                    <td class="align-middle">{{ $patient->name }}</td>
                    <td class="align-middle">{{ $patient->age }}</td>
                    <td class="align-middle">{{ $patient->gender }}</td>
                    <td class="align-middle">{{ $patient->Birthday }}</td>
                    <td class="align-middle">{{ $patient->Address }}</td>
                    <td class="align-middle">{{ $patient->Contact }}</td>
                    <td class="align-middle">{{ $patient->Guardian }}</td>
                    <td class="align-middle">{{ $patient->room }}</td>
                    <td class="align-middle">
                        <div class="btn-group" role="group" aria-label="Basic example">
                            <form method="POST" action="{{ route('patients.discharge', $patient->id) }}">
                                @csrf
                                <button type="submit">Discharge</button>
                            </form>                          
                        </div>
                    </td>
                </tr>
            @endif
        @endforeach
    @endif
</tbody>

Please update your HTML code with the correction, and it should resolve the issue. Let me know if you have any further questions!
count(): Argument #1 ($value) must be of type Countable|array, null given

ito po yung error after kopo lagyan ng
@if(count($patients) > 0)
 
count(): Argument #1 ($value) must be of type Countable|array, null given

ito po yung error after kopo lagyan ng
I apologize for the confusion. The error you're encountering occurs because the $patients variable is null, and you cannot use the count() function on a null value.

To fix this, you need to make sure that the $patients variable is assigned correctly in your controller. You can modify your showCurrentAdmit method as follows:

PHP:
public function showCurrentAdmit($room)
{
    $patients = Patient::where('room', $room)->get();

    if($patients !== null) {
        return view('patients.index', compact('patients', 'room'));
    } else {
        // Handle the case when no patients are found for the given room
        return "No patients found for the given room";
    }
}

With this updated code, if no patients are found for the given room, it will display an appropriate message instead of throwing an error.

Make sure to update your HTML code as well, like we discussed previously.

If you still encounter any issues, please let me know, and I'll be happy to assist you further.
 
I apologize for the confusion. The error you're encountering occurs because the $patients variable is null, and you cannot use the count() function on a null value.

To fix this, you need to make sure that the $patients variable is assigned correctly in your controller. You can modify your showCurrentAdmit method as follows:

PHP:
public function showCurrentAdmit($room)
{
    $patients = Patient::where('room', $room)->get();

    if($patients !== null) {
        return view('patients.index', compact('patients', 'room'));
    } else {
        // Handle the case when no patients are found for the given room
        return "No patients found for the given room";
    }
}

With this updated code, if no patients are found for the given room, it will display an appropriate message instead of throwing an error.

Make sure to update your HTML code as well, like we discussed previously.

If you still encounter any issues, please let me know, and I'll be happy to assist you further.
public function showCurrentAdmit($room)
{
$patients = Patient::where('room', $room)->get();
if($patients !== null && !$patients->isEmpty()) {
return view('patients.index', compact('patients', 'room'));
} else {
return "No patients found for the given room";
}

}

nilagyan kopo nito pero still ang error parin ay
count(): Argument #1 ($value) must be of type Countable|array, null given
 
public function showCurrentAdmit($room)
{
$patients = Patient::where('room', $room)->get();
if($patients !== null && !$patients->isEmpty()) {
return view('patients.index', compact('patients', 'room'));
} else {
return "No patients found for the given room";
}

}

nilagyan kopo nito pero still ang error parin ay
count(): Argument #1 ($value) must be of type Countable|array, null given
I apologize for the confusion. The error you're encountering occurs because you are using the count() function on a potentially null variable. Even though you have added the null check, the count() function still expects an array or an object that is countable.

To fix this issue, you can use the count() function with the null coalescing operator (??) to provide a fallback value of 0 when the $patients variable is null. Here's the updated code:

PHP:
public function showCurrentAdmit($room)
{
    $patients = Patient::where('room', $room)->get();

    if ($patients !== null) {
        return view('patients.index', compact('patients', 'room'));
    } else {
        $patientsCount = count($patients ?? []);
        if ($patientsCount > 0) {
            return view('patients.index', compact('patients', 'room'));
        } else {
            return "No patients found for the given room";
        }
    }
}

This code checks if $patients is not null, and if it's not null, it passes the value to the view. If it is null, it checks if the count of $patients is greater than 0 (using the null coalescing operator), and if so, it passes the value to the view. Otherwise, it displays the "No patients found for the given room" message.

Please give this code a try, and let me know if you have any further questions!
 
tip lang, pwede ka gumamit ng forelse para medyo malinis code
@forelse ($users as $user)
<li>{{ $user->name }}</li>
empty
<p>No users</p>
@endforelse
 

Similar threads

Back
Top