5

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 the data 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();
}
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Richard Hewitt
  • 345
  • 1
  • 5
  • 22
  • 1
    http://stackoverflow.com/questions/15935990/is-there-any-way-to-detect-if-a-database-table-exists-with-laravel – Frank Apr 09 '15 at 09:53
  • Schema::hasTable? http://laravel.com/docs/5.0/schema#checking-existence – Adrián Navarro Apr 09 '15 at 09:54
  • 3
    This question was about checking if database exists, not a table inside database. Idk why this question hasn't been answered. – Abhay Maurya Feb 01 '17 at 09:59
  • 1
    For anyone reading this now, there are better ways than running an SQL query... check this SO question https://stackoverflow.com/questions/33432752/laravel-5-1-checking-a-database-connection – matiaslauriti Mar 24 '22 at 20:48

1 Answers1

21

if anyone is still interested, it can be done as follows:

Demo controller code:

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!';
        }
   }
}
Abhay Maurya
  • 11,819
  • 8
  • 46
  • 64