如果不存在,如何插入 illuminate slim 框架

How to insert if not exist illuminate slim framework

我用的是slim framework,用的是illuminate/database.

我想插入不重复的数据 function_number 但如果不同 product_id function_number 可以重复,例如

这是我的模式

$app->post('/create_function', function($request, $reponse, $args) {
   $query = $this->db->table('functions')->insert([
            'project_id' => $request->getParam('project_id'),
            'function_number' => $request->getParam('function_number'), 
            'function_text' => $request->getParam('function_text'),
            'created' => date('Y-m-d')
         ]);
   if($query) {
     echo "Function was created";
   }
});

根据您的要求,您希望这对夫妇 (project_id, function_number) 独一无二。为此,您需要按如下方式查询您的数据库:

//Check for duplicate  
$exists = $this->db->table('functions')->where('project_id', '=', $request->getParam('project_id'))->where('function_numbe', '=', $request->getParam('function_number'))->exists();

if(!$exists) {
    //Your insert code here 
}