Creating a message of success in being able to perform a LARAVEL action 5.4

0

I'm trying to get a successful message informed on a div while an action is being performed, but I have not yet succeeded, the error that appears is:

  

(3/3) ErrorException Undefined variable: sucess (View:   /opt/lampp/htdocs/projectslaravel/clinica-beta/resources/views/layouts/app.blade.php)   (View:   /opt/lampp/htdocs/projectslaravel/clinica-beta/resources/views/layouts/app.blade.php)

My code is as follows in page layout

@if(isset($errors) and count($errors) > 0)
     <div id="msg" class="alert alert-error">                
                <p>{{$error}}</p>
     </div>           
@elseif(isset($sucess) and count($sucess) > 0)
     <div id="msg" class="alert alert-sucess">                
                <p>{{$sucess}}</p>
     </div>
@endif

in controller this way

public function ativar($id) 
{
    $apt = $this->ModelApartamento->find($id);
    $liberacao = $apt->verificarAtivar();
    if ($liberacao) {
        $dados = ['status' => 'L'];
        //dd($dados);
        $update = $apt->update($dados);
        if ($update) {
            return redirect()
                   ->back()
                   ->with('sucess','Apartamento ativado com sucesso'); 
        } else {
            return redirect()
                   ->back()
                   ->with('errors', 'Ocorreu um erro ao tentar ativar apartamento');
        }
    } else {
        return redirect()
                   ->back()
                   ->with('errors', 'Apartamento está com paciente no momento');
    }
}
    
asked by anonymous 05.09.2017 / 20:42

1 answer

0

There are several ways to make a status , to my view yours is wrong, also because your test in if does not correctly test the variables and could be done for example as follows :

create a variable of a predefined class in PHP , that is, stdClass, and in the code of controller make these changes :

public function ativar($id) 
{
    $apt = $this->ModelApartamento->find($id);
    $liberacao = $apt->verificarAtivar();

    $st = new StdClass;

    if ($liberacao) 
    {
        $dados = ['status' => 'L'];        
        $update = $apt->update($dados);
        if ($update) 
        {
            $st->status = true;
            $st->message = "Apartamento ativado com sucesso";
            return redirect()
                   ->back()
                   ->with('st', $st); 
        } 
        else 
        {
            $st->status = false;
            $st->message = "Ocorreu um erro ao tentar ativar apartamento";
            return redirect()
                   ->back()
                   ->with('st', $st);
        }
    } 
    else 
    {
        $st->status = false;
        $st->message = "Apartamento está com paciente no momento";
        return redirect()
                   ->back()
                   ->with('st', $st);
    }
}

@if(session()->has('st'))
@if(!session()->get('st')->status)
    <div id="msg" class="alert alert-error">                
        <p>{{$st->message}}</p>
    </div>           
@else
    <div id="msg" class="alert alert-sucess">                
        <p>{{$st->message}}</p>
    </div>
@endif
@endif

could still look better on your View as follows:

@if(session()->has('st'))
<div 
  id="msg" 
  class="@if(session()->get('st')->status){{'alert alert-sucess'}}@else{{'alert alert-error'}}@endif">
    <p>{{$st->message}}</p>
</div>
@endif

this would be one of the ways.

05.09.2017 / 21:38