Can I display a message from return ->with() in laravel?

Kaiserr :

For example, I have a function and can I somehow display this message that the body should not be empty, display it on the screen?

public function store(Request $request){
        $comment = new Comment;
        $comment->body = $request->get('comment_body');
        if(empty($comment->body)){
            return back()->with('Body should not be empty! ');
        }else{
            $comment->user()->associate($request->user());
            $post = Article::find($request->get('article_id'));
            $post->comment()->save($comment); 
            return back();
        }

    }
Lucas Piazzi :

You can redirect using flashed data

Redirecting to a new URL and flashing data to the session are usually done at the same time. Typically, this is done after successfully performing an action when you flash a success message to the session. For convenience, you may create a RedirectResponse instance and flash data to the session in a single, fluent method chain:

So your controller should look like this:

        if(empty($comment->body)){
            return back()->with('error', 'Body should not be empty! ');

And in your view you can display the error like this:

@if (session('error'))
    <div class="alert alert-success">
        {{ session('error') }}
    </div>
@endif

But i strongly recommend you to read the validation section of the laravel documentation. You can achieve better results if you create a request or use the validate method

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In Laravel, how can i return a specific *column* from a *table* inside my API Controller ? I'm using Laravel 5

How can I return a byte array as an image from a Spring Controller, so that a browser can display it?

Can I delete the message field from Logstash?

How to display a success message upon redirection from controller in Laravel?

Can i return to a view from href tag in laravel without using controller?

How Can I return two variables from same method/function to one view in laravel 5.4?

How to display image sent from Laravel in email message

How can I return the required message without the distinct message?

How can I return error message from axios function inside Vuex action to dispatch catch(error)?

How can i display grouped values in Laravel

How i can display image from path Laravel?

How can I return a customized error message from a failed promise in a Google Firebase function?

How can i store image and display it in webpage from database in laravel 4

I want my return from SQL to display in a message box in C#

How can I display json data return from the controller using angularjs

How can I display required message in laravel?

How can I retrieve the ex Exception object from session state and display the Message property of the exception?

how can i display my records from database in datatables using laravel

How can i return warning message from .NET CORE API

How to return and display an error message from within a exception try catch

can i Filtering number from a message?

What I not writing correctly that I can not display message in the alert/console?

How can I display pdf file from local public/uploads folder in laravel into html <iframe>

How can I dynamically display Total number of items from the Database in a blade view in Laravel 7

How can I return a message if there are no results from a query but display results if there are?

How do I display my error message as a link in Laravel

How can I return post API message?

Can I change a Laravel error message globally?

How can i call specific data from database to add in my with message in Laravel