How to call a controller method via JS or Ajax in Windows

Asked

Viewed 428 times

-2

I’m new with PHP and Laravel, besides I don’t know much about JS.

I have the following question, I have a view that displays the information of the service provider of my project, before this guy is able to work I need to validate his registration, so I valid the information, if they are correct I want to click an Approve button that via JS or AJAX will call a method from the provider controller and will change its status within the system, but I do not know how to do this, someone has some example so I can base myself?

View:

@extends('templates.template-admin')
@section('content')
    <h1 class="text-center">Informações do prestadores</h1>
    <div class="col-8 m-auto">
        @csrf

        {{-- Fazendo select para encontrar o endereço e a cidade usando o relacionamento feito entre as tabelas --}}
        @php
            $endereco = $prestadores->find($prestadores->ID)
                                    ->relEndereco;  

            $cidade = $enderecos->find($enderecos->ID)
                                ->relCidade;     
                               
            $certificado = $prestadores->find($prestadores->ID)
                                    ->relCertificado;  

        @endphp

        Nome:{{$prestadores->NOME}}<br>
        CPF:{{$prestadores->CPF}}<br>
        Telefone:{{$prestadores->TELEFONE}}<br>
        Data de Nascimento:{{$prestadores->DT_NASCIMENTO}}<br>
        E-mail:{{$prestadores->EMAIL}}<br>
        CEP:{{$endereco->CEP}}<br>
        Endereço:{{$endereco->ENDERECO}}<br>
        Número:{{$endereco->NUMERO}}<br>
        Bairro:{{$endereco->BAIRRO}}<br>
        Cidade:{{$cidade->CIDADE}}<br>
        Estado:{{$cidade->UF}}<br>
        @if ($certificados->CERTIFICADO)
            Certificado:<a href="{{url("storage/{$certificados->CERTIFICADO}")}}" target="_blank">
                <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-file-earmark-medical" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                    <path d="M4 1h5v1H4a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V6h1v7a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2z"/>
                    <path d="M9 4.5V1l5 5h-3.5A1.5 1.5 0 0 1 9 4.5z"/>
                    <path fill-rule="evenodd" d="M7 4a.5.5 0 0 1 .5.5v.634l.549-.317a.5.5 0 1 1 .5.866L8 6l.549.317a.5.5 0 1 1-.5.866L7.5 6.866V7.5a.5.5 0 0 1-1 0v-.634l-.549.317a.5.5 0 1 1-.5-.866L6 6l-.549-.317a.5.5 0 0 1 .5-.866l.549.317V4.5A.5.5 0 0 1 7 4zM5 9.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5zm0 2a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5z"/>
                </svg>
            </a>
        @endif
        <br>
        @if ($antecedentes->ANTECEDENTE)
            Antedecentes:<a href="{{url("storage/{$antecedentes->ANTECEDENTE}")}}" target="_blank">
                <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-file-earmark-medical" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                    <path d="M4 1h5v1H4a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V6h1v7a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2z"/>
                    <path d="M9 4.5V1l5 5h-3.5A1.5 1.5 0 0 1 9 4.5z"/>
                    <path fill-rule="evenodd" d="M7 4a.5.5 0 0 1 .5.5v.634l.549-.317a.5.5 0 1 1 .5.866L8 6l.549.317a.5.5 0 1 1-.5.866L7.5 6.866V7.5a.5.5 0 0 1-1 0v-.634l-.549.317a.5.5 0 1 1-.5-.866L6 6l-.549-.317a.5.5 0 0 1 .5-.866l.549.317V4.5A.5.5 0 0 1 7 4zM5 9.5a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5zm0 2a.5.5 0 0 1 .5-.5h5a.5.5 0 0 1 0 1h-5a.5.5 0 0 1-.5-.5z"/>
                </svg>
            </a>
        @endif
        <br><br>
        <a href="">
            <button class="btn btn-success">Aprovar</button>
        </a>
        <a href="">
            <button class="btn btn-danger">Reprovar</button>
        </a>
    </div>
@endsection

Controller method I want to call:

public function aprovar($id)
    {
        
        // Pegando o valor da constant para colocar no prestador
        // $statusAprovado = \Config::get('constants.STATUS.ATIVO');
        // $status = App\Models\prestadores::find($id);
        // $status->STATUS = $statusAprovado;
        // $status->save();
    }

1 answer

1

In your project or in the Blade file you can call a file or it can even be within the code a Javascript snippet.

But before calling you can import a file or use a CDN from a Jquery to make it easier:

<script>
  $.ajax({
      url: 'http://localhost/suarota/1',
      type: 'put',
      dataType: 'json'      
   }).then( response =>{
       //o retorno do processamento
   })
</script>

on the route you prepare to receive the code sent by ajax

Route::put('/suarota/{id}', 'HomeController@aprovar');

Don’t forget to change the route and code you need to change

I hope you gave a light

  • You’ve helped me so much, thank you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.