所以我在这个网站上你可以购买游戏机,所以我做了一个小问题,但是我想要在数据库中插入重复项是不可能的,但是当我创建一个副本时,你会得到一个laravel错误这不是用户友好的。所以我想显示一条正常的消息说你复制了。所以我做到了
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');
}
问题是 - > withErrors(数组('消息' =>'重复'))并没有显示任何内容。我做错了什么。
修改
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>
答案 0 :(得分:1)
您只需使用with()
将消息发送到视图,如
return Redirect::to('console/create')
->withInput()
->with('message', 'duplicate');
并在视图中访问
@if ($message = Session::get('message'))
{{$message}}
@endif
答案 1 :(得分:0)
当您设置withErrors
时,您不需要传递数组,只需写下错误消息->withErrors('Duplicate');
在视图中记得要检查是否有错误
@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