<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public static function createOrUpdate($tableName, array $values = [], $includeTimestamps = true)
    {
        $record = DB::table($tableName)->where($values)->first();
        if (is_null($record))
        {
            // Add timestamps if we're creating a record
            if ($includeTimestamps && !isset($values['created_at']))
            {
                $values['created_at'] = new \DateTime();
            }

            if ($includeTimestamps && !isset($values['updated_at']))
            {
                $values['updated_at'] = new \DateTime();
            }

            DB::table($tableName)->insert($values);
            $record = DB::table($tableName)->where($values)->first();
        }

        return $record;
    }

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(PermissionsSeeder::class);
    }
}