2015-04-16 156 views
1

這裏是我的視圖代碼:laravel請求驗證問題

<!-- 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">&times;</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 []; 
} 
// ... 

後發送POST請求創建方法,提高

SQLSTATE [42S22 ]:未找到列:1054未知列'article.title' in'where clause'

SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'article.title' (SQL:select count(*)as articles where articletitle =試驗)


更新

Here`s我的控制器的代碼:

// 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

它清楚地說明什麼問題'未知列'article.title''意味着你沒有名爲'article.title'的列,因爲它應該是文章** s **。title – Gal

+0

你能顯示控制器存儲方法的代碼? –

+0

刪除雙重代碼 –

回答

1

更改您的規則在StoreArticleRequest.php文件

case 'POST': { 
    return [ 
    'article.title' => 'required|unique:articles,title|max:255', 
    'article.body' => 'required' 
    ]; 
} 

,如果你不是在獨特驗證指定的列名這需要考慮關鍵作爲列名。

+0

似乎我需要閱讀文檔更多。謝謝。 – winterwhisper

0

,而不是使用第[標題]作爲輸入名稱使用標題作爲輸入的名稱和在規則中使用標題而不是文章。標題

+0

將'article.title'更改爲'article [title]'後,驗證失敗。 – winterwhisper

+0

只在規則用戶'標題'和輸入字段用戶標題只有像