为什么laravel在插入时运行选择查询

时间:2017-11-14 15:05:29

标签: php mysql laravel-5.5

我试图将数据插入到我的数据库中,但我一直在收到此错误,因为我删除了一个名为categories的表,该表与另一个表具有雄辩的关系

有趣的是,Laravel正在从一个删除的表中运行一个select查询,而我的代码是一个插入查询..

{
"message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'train.categories' doesn't exist (SQL: select count(*) as aggregate from `categories` where `name` = Oil Drilling)",
"exception": "Illuminate\\Database\\QueryException",
"file": "C:\\xampp\\htdocs\\enter code heretrain\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php",
"line": 664,
"trace": [
    {
        "file": "C:\\xampp\\htdocs\\train\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php",
        "line": 624,
        "function": "runQueryCallback",
        "class": "Illuminate\\Database\\Connection",
        "type": "->"
    },
    {
        "file": "C:\\xampp\\htdocs\\train\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php",
        "line": 333,
        "function": "run",
        "class": "Illuminate\\Database\\Connection",
        "type": "->"
    },

]

}

这是我的插入代码

 public function store(Request $request)
{
     $request->validate([
        'name' => 'required|string|max:250|unique:categories',
        'image' => 'required|image',
        'price' => 'required',
        'duration' => 'required',
        'description' => 'required',
        'url' => 'string'
    ]);
    if ( $request->hasFile('image') ) {

        $ext = $request->image->extension();
        $iconName =  str_slug('storage/'.$request->name).'.'.$ext;
        $request->image->storeAs('public',$iconName);

        DB::table('courses')->insert(
            [
                'name' => $request->name,
                'image' => $iconName,
                'url' => $request->url,
                'price' => $request->price,
                'duration' => $request->duration,
                'description' => $request->description
            ]
        );
        return response()->json([
            'success'=>true,
            'image' => $iconName
        ]); 
    }
    return response()->json(['error' => false]); 
}

1 个答案:

答案 0 :(得分:0)

检查以下行中的唯一:类别

名称' => '需要|串|最大:250 |独特:类别

你可能知道它是什么......但是让我解释一下 唯一将检查给定名称已存在于类别表中。有点你没有那张桌子。所以它会给你那个错误