0

i am trying to make custom message bag but unable to make, i want to use the $errors

$errorMsgs = [];
    if (Model::where('name', '=', Input::get('name'))->exists()) {
       $errorMsgs['company_name'] = 'name already exists';

    }else if (Model::where('age', '=', Input::get('age'))->exists()) {
        $errorMsgs['primary_phone'] = 'age no already exists';

    }else if (Model::where('mobile', '=', Input::get('mobile'))->exists()) {
        $errorMsgs['primary_mobile'] = 'mobile already exists';

    }else if (Model::where('pri_email', '=', Input::get('primary_email'))->exists()) {
        $errorMsgs['primary_email'] = 'Primary email already exists';

    }else{
// success 


        }

Actually i want to use errors foreach

 @if ($errors->any())
               <div class="alert alert-danger">
                   <ul>
                       @foreach ($errors->all() as $error)
                           <li>{{ $error }}</li>
                       @endforeach
                   </ul>
               </div>
           @endif
4
  • \Session::flash('errors', $array) should cover it, no? however I'd suggest using a validator instead - laravel.com/docs/5.6/validation, they're for that purpose and very simple to implement Commented Jun 15, 2018 at 23:14
  • i am not doing form validation i am checking duplicate entry, if any duplicate entry found then errors should return in message bag Commented Jun 15, 2018 at 23:18
  • Validator has a unique property for that purpose laravel.com/docs/5.6/validation#rule-unique Commented Jun 15, 2018 at 23:19
  • you are not getting, please read the question again i want to create message bag for errors then i will echo using foreach Commented Jun 15, 2018 at 23:29

1 Answer 1

3

I cannot see from your question why the out-the-box unique Validation is not being used, it's for seemingly exactly this purpose. Anyway, the gist of the manual way would be something like...

// Controller
public function WhatEverController()
{
    $data = [];

    $errorMsgs = [];
    if (Model::where('name', '=', Input::get('name'))->exists()) {
       $errorMsgs['company_name'] = 'name already exists';

    }
    if (Model::where('age', '=', Input::get('age'))->exists()) {
        $errorMsgs['primary_phone'] = 'age no already exists';

    }
    if (Model::where('mobile', '=', Input::get('mobile'))->exists()) {
        $errorMsgs['primary_mobile'] = 'mobile already exists';

    }
    if (Model::where('pri_email', '=', Input::get('primary_email'))->exists()) {
        $errorMsgs['primary_email'] = 'Primary email already exists';
    }

    if(count($errorMsgs)) {
        return  redirect()->back()->withErrors($errorMsgs);
    }

    return view('someview');
}


// Blade
@if(count($errors))
<div class="alert alert-danger">
    <ul>
        @foreach ($errors as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

One more detail, with your if/elseif use, it will always stop going through on the first match therefore always a maximum of one $errorMsg. I have changed it to a number of if statements as this will provide the opportunity to obtain an array of errors.

Sign up to request clarification or add additional context in comments.

7 Comments

i tried your code and it throw error ErrorException (E_ERROR) Call to a member function any() on array (View: /var/www/html/cb/resources/views/frontend/company/user/index.blade.php)
okay you have done great you are very near i need the same thing but when we have errors in array i want to echo them but using the php html which i post above
how can i done this with laravel validation you have mentioned above
Amended the controller return, please retry
you made my day :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.