user9806487
user9806487

Reputation:

laravel using withErrors in a try catch

So i'm making this website where you can buy game consoles, so I made a crud, but I wanted that it was impossible to insert duplicates in the database, but when I create a dupicate you get a laravel error and that is not user friendly. So I wanted to show a normal message saying that you made a duplicate. so I made this.

public function store(Request  $request)
{
    try
    {
        $consoles = new consoles();
        $consoles->naam = input::get('naam');
        $consoles->releasedate = input::get('releasedate');
        $consoles->company = input::get('company');
        $consoles->price = input::get('price');
        $consoles->created_at = null;
        $consoles->updated_at = null;
        $consoles->save();
    }catch (\Exception $e)
    {
        return Redirect::to('console/create')
            ->withInput()
            ->withErrors(array('message' => 'duplicate'));

    }

    return redirect('consoles');
}

the problem is that ->withErrors(array('message' => 'duplicate')) doesn't show anything. what am I doing wrong.

EDIT

create.blade.php

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
    $( function() {
        $( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' }).val();
    } );
</script>
</head>
<body>
<div class="container">
<div class="row">
    <div class="col-md-10 col-md-offset-1">
        <div class="panel panel-default">
            <div class="panel-heading"> creating data</div>
            <form method="POST" action="{{url('games/store/')}}">
                naam: <br>
                <input type="text" name="naam" required>*required<br>
                releasedate: <br>
                <input type="text" name="releasedate" id="datepicker" required>*required<br>
                company: <br>
                <input type="text" name="company" required>*required<br>
                price: <br>
                <input type="number" name="price" min="0" value="0" step=".01" required>*required<br>
                <input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
                <input type="submit" name="create" value="create">
            </form>
        </div>
    </div>
</div>
</div>
</body>
</html>

Upvotes: 2

Views: 575

Answers (2)

AntonyMN
AntonyMN

Reputation: 921

When you set withErrors you don't need to pass the array, just write the error message like this ->withErrors('Duplicate');

In the view remember to include a check if there are errors

@if ($errors->count())
 <div class="col-md-12">
  <div class="alert alert-danger text-center">
   @foreach ($errors->all() as $error)
     <p>{{$error}}</p>
   @endforeach
  </div>
 </div>
@endif

Upvotes: 2

Sujan Gainju
Sujan Gainju

Reputation: 4769

You can simply use with() to send the message to the view like

 return Redirect::to('console/create')
            ->withInput()
            ->with('message', 'duplicate');

and access that in view as

@if ($message = Session::get('message'))
  {{$message}}
@endif

Upvotes: 1

Related Questions