forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlUser.php
More file actions
68 lines (56 loc) · 1.91 KB
/
Copy pathSqlUser.php
File metadata and controls
68 lines (56 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
namespace MongoDB\Laravel\Tests\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Support\Facades\Schema;
use MongoDB\Laravel\Eloquent\HybridRelations;
use function assert;
class SqlUser extends EloquentModel
{
use HybridRelations;
protected $connection = 'sqlite';
protected $table = 'users';
protected static $unguarded = true;
public function books(): HasMany
{
return $this->hasMany(Book::class, 'author_id');
}
public function role(): HasOne
{
return $this->hasOne(Role::class);
}
public function skills(): BelongsToMany
{
return $this->belongsToMany(Skill::class, relatedPivotKey: 'skills');
}
public function sqlBooks(): HasMany
{
return $this->hasMany(SqlBook::class);
}
/**
* Check if we need to run the schema.
*/
public static function executeSchema(): void
{
$schema = Schema::connection('sqlite');
assert($schema instanceof SQLiteBuilder);
$schema->dropIfExists('users');
$schema->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
if (! $schema->hasTable('skill_sql_user')) {
$schema->create('skill_sql_user', function (Blueprint $table) {
$table->foreignIdFor(self::class)->constrained()->cascadeOnDelete();
$table->string((new Skill())->getForeignKey());
$table->primary([(new self())->getForeignKey(), (new Skill())->getForeignKey()]);
});
}
}
}