LoginSignup
0
1

More than 3 years have passed since last update.

【Laravel】バリデーションルールの書き方のミスでSQLエラー “SQLSTATE[42S22]: Column not found:”

Posted at

phpMyAdminでは存在するカラム名がないと言われる

SQLSTATE[42S22]: Column not found: 1054 Unknown column ' image_path' in 'where clause'

image_pathというカラムがありませんよ
って言われているけど、phpMyAdminで確認するとちゃんとある。

バリデーションルールの書き方が間違っている?

CharacterRequest.php
    public function rules()
    {
        return [
            'name' => 'required | max:100',
            'image_path' => 'required | unique:characters', // uniqueの第2引数にimage_pathを入れていた 
            'genre_id' => 'required',
        ];
    }

どうやらuniqueの第2引数にimage_pathを入れていたのが間違いだったよう。
消したらエラーが出なくなりました。

第2引数はカラム名

https://readouble.com/laravel/5.8/ja/validation.html
image.png

公式の説明では上記のように書いていますが、フィールド名とカラム名の違いがわかっていません。。。

とりあえず、同じものが挿入されないようにするなら

unique:テーブル名

としておけば良さそうです。

素人記事ですが、一応これでバリデーションはうまく行きました。

別の原因で発生することもあるようなので、他の記事も参考にしてください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1