Creating a message of success when able to perform an action LARAVEL 5.4

Asked

Viewed 3,200 times

0

I’m trying to get you informed a message of success in a div when an action is carried out, however I could not yet, the error that appears is:

(3/3) Errorexception Undefined variable: sucess (View: /opt/lampp/htdocs/projetoslaravel/clinica-beta/Resources/views/layouts/app.blade.php) (View: /opt/lampp/htdocs/projetoslaravel/clinica-beta/Resources/views/layouts/app.blade.php)

my code is as follows on the 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 the controller as follows

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');
    }
}
  • The variable has not been set, it is set in the controller if there is success of the update, then the correct in your Test is to do "if it is error show otherwise show success" and you are asking for error and success.

1 answer

0


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

Create a variable from a predefined class in PHP, i.e., 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);
    }
}

and in the View also make these changes:

@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 get better at 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

that would be one of the ways.

References

  • @Gabrielrodrigues neh! If true it reverses and falls on Is that in the case of the answer is success? correct?

  • I’m not getting the value of this variable in the view , in case it’s redirecting to index.blade.php , is there any other way I don’t have to keep creating an instance of Stdclass? in case I’m thinking about it for a larger system with many Ruds and many success or error messages

  • @Márciocésar doesn’t need to change this form, maybe your context is bigger and this hinders, for example how you use the code and why you are using back()?

  • this action activating this being shown in index , I use the back() to go back to index() however when sending with('st',$st) in template.blade.php it says that the variable is not defined the template is being called in the index file tbm.

  • try it this way I edit @Márciocésar (withInput) take a look at the answer

  • @Márciocésar what is the name of the route of the page index.blade.php?

  • route is 'apartments.index'

  • Actually there is a session I until I got confused take a look at the issue @Márciocésar

  • I got here brother , thank you very much, I am thinking of a solution to not need to always declare the variable $st and not instantiating it . has tips ?

  • Once I made a class that depending on the event was characterized by standard messages ... would be an instance of the class but, I used in a way that does not need to be instilling the very Port by the service Provider did this... @Márciocésar

Show 5 more comments

Browser other questions tagged

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