From 30c3079a90e44c7d51f1a640846eeb8c0aaf1d11 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 12 Oct 2018 15:10:40 +0100 Subject: [PATCH] added setting user many to many relationship --- app/Setting.php | 9 +++++ app/User.php | 8 +++++ ...140451_create_setting_user_pivot_table.php | 35 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 database/migrations/2018_10_12_140451_create_setting_user_pivot_table.php diff --git a/app/Setting.php b/app/Setting.php index 706e1eb7..b981d0d2 100644 --- a/app/Setting.php +++ b/app/Setting.php @@ -218,4 +218,13 @@ class Setting extends Model } return $output; } + + /** + * The users that belong to the setting. + */ + public function users() + { + return $this->belongsToMany('App\User'); + } + } diff --git a/app/User.php b/app/User.php index 7c7d7ea1..fb9f8fdd 100644 --- a/app/User.php +++ b/app/User.php @@ -35,4 +35,12 @@ class User extends Authenticatable return $this->hasMany('App\Item'); } + /** + * The settings that belong to the user. + */ + public function settings() + { + return $this->belongsToMany('App\Setting'); + } + } diff --git a/database/migrations/2018_10_12_140451_create_setting_user_pivot_table.php b/database/migrations/2018_10_12_140451_create_setting_user_pivot_table.php new file mode 100644 index 00000000..94cd1cd0 --- /dev/null +++ b/database/migrations/2018_10_12_140451_create_setting_user_pivot_table.php @@ -0,0 +1,35 @@ +integer('setting_id')->unsigned()->index(); + $table->foreign('setting_id')->references('id')->on('settings')->onDelete('cascade'); + $table->integer('user_id')->unsigned()->index(); + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + $table->primary(['setting_id', 'user_id']); + $table->string('value')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('setting_user'); + } +}