laravel check if it exists and send an alert

Viewed 835

I need help with Laravel

I need to get a code that runs: if and send a message ::

 $text = App\Nota::select('nombre')->where('id', 3);

I need to check that if a string = 'hello' arrives it will print a message in .blade

in short check if it exists and send an alert

Help me please

2 Answers

you can do in this way.

$text = App\Nota::select('nombre')->where('id', 3);if($text->nombre=="hello"){Session::flash('message', 'This is a message!');Session::flash('alert-class', 'alert-danger'); }

In the view you can make it display like this.

@if(Session::has('message'))<p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ Session::get('message') }}</p>@endif

Try this way using exist() method if you just want to check record is exists or not. Here, in your case, you just want to check that modal Nota having id 3 and it's nombre column should be equal to hello. There is no need to load the modal, you can try specifying both conditions in where() method and use exist() to check the record exist.

Controller

$exist = App\Nota::where([
    'id' => 3,
    'nombre => 'hello'
])->exists();

if(!$exist){
    Session::flash('message', 'This is a message!'); 
    Session::flash('alert-class', 'alert-danger'); 
}

View

@if(Session::has('message'))
<p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ 
    Session::get('message') }}</p>
@endif
Related