本文介绍了使用Laravel检查数据库是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Laravel编写变更日志,并被要求从MySQL数据库中将数据提取到数组中;根据从JSON文件读取的值,检查数组中是否存在帐户ID.
I am trying to write a changelog using Laravel and have been asked to pull data from a MySQL database into an array; check in the array, if an account ID exists based on values read in from JSON files.
如果不存在,则需要创建它并将其添加到数组中.
If not present, I need to create it and add to the array.
我目前所拥有的代码将条目添加到数据库中,但是它不执行任何检查.我的代码如下:
The code I have at the moment adds entries to the database, but it does not do any sort of checking; my code is as follows:
if(isset($cfi->awsAccountId)) {
$aid= new Account;
$aid->aws_account_id = $cfi->awsAccountId;
$aid->save();
}
推荐答案
如果仍然有人感兴趣,可以按照以下步骤进行操作:
if anyone is still interested, it can be done as follows:
演示控制器代码:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class Testing extends Controller
{
public function get()
{
$query = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?";
$db = DB::select($query, [$your_database_name]);
if (empty($db)) {
echo 'No db exist of that name!';
} else {
echo 'db already exists!';
}
}
}
这篇关于使用Laravel检查数据库是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!