gpt4 book ai didi

php - 为什么 Laravel 在插入时运行选择查询

转载 作者:行者123 更新时间:2023-11-29 18:12:27 24 4
gpt4 key购买 nike

我正在尝试将数据插入数据库,但自从我删除了一个名为categories的表,该表与另一个表有很好的关系后,我一直收到此错误

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

{
"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]);
}

最佳答案

检查下面一行的 unique:categories

名称' => '必需|字符串|最大:250|唯一:类别

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

关于php - 为什么 Laravel 在插入时运行选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47289013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com