Home » Php » Laravel 5.4 – Error Message Without Form Validation

Laravel 5.4 – Error Message Without Form Validation

Posted by: admin October 26, 2017 Leave a comment

Questions:

Want to know if I can use the validation error message on Laravel 5.4 with my custom error message that does not include a form validation.

I am making a validation on view user profile that is to make sure only the owner of the id can view the user profile using Gate.

Do I need to create the instance of Validator and do something like on the Laravel docs.

Below are the example of my codes,

UserController.php

public function show(User $user)
{
    if (\Gate::denies('showUser', $user)) {
        return redirect()->back()
        ->with('status', 'Invalid Access to other User Profile');
        // how to use instead ->withErrors($message); ?
    }

    return view('profiles.update', compact('user'));
}

messages.blade.php

@if(count($errors) > 0)
    <div class="alert alert-danger" role="alert">

        <ol>

        @foreach($errors->all() as $error)  

                <li>{{ $error }}</li>           

        @endforeach

        </ol> 

    </div>
@endif

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

@if(session('status'))
    <div class='alert alert-danger'>
        {!! session('status') !!}
    </div>
@endif
Answers: