1

i'am using Laravel in my project so i want to change the status of patient when he take an appointement with doctor , so he can have 3 satuts , Accept , waiting (automatically when the patient choose the date and hour), not accept , the issue i'am facing is when i click on active button nothing change in database.

This is the controller of status:

public function changeStatus(Request $request)
{
    $user = rdv::where('IDP', $request->input('IDP'))->get();
    
    if (count($user) > 0) {
        if ($user->Etat_de_rdv == en_attente) {
            $user->Etat_de_rdv = Accepté;
        } else {
            $user->Etat_de_rdv = Reservé;
        }

        $user->Etat_de_rdv = $request->input('status');
        $user->save();

        return response()->json(['success' => 'Status change successfully.']);
    }
}

This is the script :

  <script>

    $('.toggle-class').change(function () {
        var status = $(this).prop('checked') == true ? 'Accepter' : 'Temps charger';
        var id = $(this).data('IDP');
        console.log(id);
        console.log(status);
        $.ajax({
            type: "get",
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            url: 'changeStatus',
            data: {
                Etat_de_rdv: status, //Should be like this
                IDP: id
            },
            success: function (data) {
                console.log(data.success);
            }
        });
    });
</script>

This is the view witch the doctor see all the appointement ;

<h3> Vos patient :</h3>
@foreach($pat as $lo)
    @if ($lo->IDD== $med->ID)
        <div class="admin">
            <div class="info">
                <h3> {{ $lo->Nom_et_prénom  }} </h3>
                <p>{{ $lo->Numéro_de_téléphone }}</p>
                <p>{{ $lo->date}}</p>
                <p>{{ $lo->time }}</p>
                <input data-id="{{$lo->IPD}}" class="toggle-class" type="checkbox" data-onstyle="success"
                       data-offstyle="danger" data-toggle="toggle" data-on="Accepter"
                       data-off="Temps charger" {{ $lo->Etat_de_rdv ? 'checked' : '' }}>
            </div>
        </div>
    @endif
@endforeach 

*rdv mean : appointement table *IDP : id of patient

5
  • Is it Accepté or "Accepté" ? in your controller
    – Vinay
    Commented Jun 21, 2020 at 12:26
  • ow i forget the " " @Viney
    – Lucia
    Commented Jun 21, 2020 at 12:32
  • Nothing change @Viney
    – Lucia
    Commented Jun 21, 2020 at 12:33
  • Could you please check in devtool whether any ajax request emerges when you click on active button
    – Vinay
    Commented Jun 21, 2020 at 12:43
  • Please can you check the network tab in your browser to confirm that the AJAX request is definitely going to the correct url (and whether or not it's returning a 200 status). Can you also added the code from your routes/web.php file please.
    – Rwd
    Commented Jun 21, 2020 at 12:55

1 Answer 1

0

->get(); returns a collection, not an array. Therefore, you should use $user->count() > 0 instead.

Edit: ->get() returns a collection of objects. So, you simply cannot use $user->Etat_de_rdv == en_attente. There are 2 ways to solve this.

  1. If you are certain that where IDP condition is always gonna return a single item, then you are better of using ->first() instead of ->get() and you won't need to change the inner if condition.

  2. If there are gonna be multiple users, then first rename the variable to $users and then loop it like this $foreach($users as $user) { // your current code... }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.