obernal
obernal

Reputation: 33

Refresh session values in view Laravel

I want to refresh the session data in the view without a reloading view.

I have a POST method with ajax:

$.ajax({
       type: "POST",
       url: '/recargarTipoCentro',
       data: {
           entidad_id: $('#entidad').val(),
           region_sanitaria_id: $('#region_sanitaria').val(),
           territorio_id: $('#territorio').val(),
           sap_id: $('#sap').val(),
           up_servicio_id: $('#up_servicio').val()
       },
       success: function(data) {
            if (!data.result) {
               alert('Hi ha hagut un error');
               return false;
            }

            $.each(data.response.tipo_centro, function(i, e) {
                  $('#tipo_centro').append('<option value="' + e.id + '">' + e.descripcio + '</option>');
            });

            @if( session()->has('tipo_centroCalendario') && !empty(session()->get('tipo_centroCalendario')))
                $('#tipo_centro').val('{{ session()->get('tipo_centroCalendario') }}');
                $('#tipo_centro').change();
            @else
                $('#tipo_centro').val('todos');
            @endif
        }
  });

And this is the controller where i forget and put session data:

    /** RECARGAR CENTROS EN EL SELECTOR */
    public function recargarTipoCentro(Request $request){


        $up_servicios_id    = $request['up_servicio_id'];

        Session::put('up_servicioCalendario', $up_servicios_id);
        Session::forget('tipo_centroCalendario');
        Session::forget('centroCalendario');
        Session::forget('ubicacionCalendario');

        $up_servicios   = DB::table('gen_a_up_servei')
                        ->where('UP_ID_UP', $up_servicios_id)
                        ->distinct()->pluck('UP_CODI_LDAP')->toArray();
        
        $centros        = DB::table('gen_a_centres')
                        ->select('tipus')
                        ->where('CODIGO_EXT', $up_servicios)
                        ->whereNull('DATA_BAIXA')
                        ->distinct()->pluck('tipus')->toArray();

        $tipo_centro    = DB::table('gen_a_centres_tipus')
                        ->orderBy('descripcio')
                        ->select('id', 'descripcio')
                        ->whereIn('id', $centros)
                        ->get()->toArray();

        return response()->json(['result' => true, 'response' => array('tipo_centro' => $tipo_centro)]);
    }

I tried to delete session data using Session::forget() on the controller where i make the ajax petition but don't works.

Upvotes: 1

Views: 342

Answers (1)

obernal
obernal

Reputation: 33

I solved my question.

We can't do it, session data can't be refreshed in the view withouth refresh the web page.

Upvotes: 0

Related Questions