这是我的观看代码:
<!-- form.blade.php -->
<form class="form-horizontal" action="{{ isset($article) ? URL('console/articles/'.$article->id) : URL('console/articles') }}" method="POST">
@if (isset($article))
<input name="_method" type="hidden" value="PUT">
@endif
@unless (empty($errors->first()))
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
...
</div>
@endunless
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
<label for="article_title" class="col-sm-2 control-label">...</label>
<div class="col-sm-8">
<input type="text" name="article[title]" id="article_title" class="form-control" placeholder="..." value="{{isset($article) ? $article->title : old('title')}}">
{!! $errors->first('title', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('body') ? 'has-error' : '' }}">
<label for="article_body" class="col-sm-2 control-label">...</label>
<div class="col-sm-8">
<textarea name="article[body]" id="article_body" cols="30" rows="10" class="form-control">{{isset($article) ? $article->body : old('body')}}</textarea>
{!! $errors->first('body', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<button type="submit" class="btn btn-success">...</button>
</div>
</div>
</form>
并请求验证人:
// StoreArticleRequest.php
// ...
public function rules()
{
switch ($this->method()) {
case 'GET':
case 'DELETE': {
return [];
}
case 'POST': {
return [
'article.title' => 'required|unique:articles|max:255',
'article.body' => 'required'
];
}
case 'PUT':
case 'PATCH': {
return [
'article.title' => 'required|unique:articles,title,'.Route::input('articles').'|max:255',
'article.body' => 'required'
];
}
default:
break;
}
return [];
}
// ...
在发送创建方法的请求后,它会引发
SQLSTATE [42S22]:未找到列:1054未知列&#39; article.title&#39; 在&#39; where子句&#39;
SQLSTATE [42S22]:未找到列:1054未知列&#39; article.title&#39; 在&#39; where子句&#39; (SQL:从
articles
选择count(*)作为聚合 其中article
。title
= test)
更新
这是我的控制器代码:
// ArticlesController.php
// ...
public function store(StoreArticleRequest $request)
{
$article = new Article($request->input('article'));
if ($article->save()) {
return redirect('console/articles')->with('success', '...');
} else {
return redirect()->back()->withInput();
}
}
// ...
public function update(StoreArticleRequest $request, $id)
{
$article = Article::find($id);
if ($article->update($request->input('article'))) {
return redirect('console/articles')->with('success', '...');
} else {
return redirect()->back()->withInput();
}
}
// ...
答案 0 :(得分:2)
在 StoreArticleRequest.php 文件
中更改规则case 'POST': {
return [
'article.title' => 'required|unique:articles,title|max:255',
'article.body' => 'required'
];
}
如果您未在唯一验证中指定列名,则需要将键视为列名。
答案 1 :(得分:0)
而不是使用文章[title]作为输入名称使用title作为输入名称,而在规则中使用title而不是article.title