Backend for songs.zachdecook.com
* Playlists: Set up many to many relationship
Zach DeCook 2018-12-27
parent 793f997 · commit 67c377f
-rw-r--r--laravel/app/Playlist.php14
-rw-r--r--laravel/app/Song.php5
-rw-r--r--laravel/database/migrations/2018_12_27_052646_create_playlist_table.php41
3 files changed, 59 insertions, 1 deletions
diff --git a/laravel/app/Playlist.php b/laravel/app/Playlist.php
new file mode 100644
index 0000000..7284b0e
--- /dev/null
+++ b/laravel/app/Playlist.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Playlist extends Model
+{
+ public $fillable = ['name'];
+ public function songs()
+ {
+ return $this->belongsToMany('App\Song');
+ }
+}
diff --git a/laravel/app/Song.php b/laravel/app/Song.php
index 876617f..0565ff6 100644
--- a/laravel/app/Song.php
+++ b/laravel/app/Song.php
@@ -7,5 +7,8 @@ use Illuminate\Database\Eloquent\Model;
class Song extends Model
{
public $fillable = ['number', 'title', 'author', 'key', 'text'];
- //
+ public function playlists()
+ {
+ return $this->belongsToMany('App\Playlist');
+ }
}
diff --git a/laravel/database/migrations/2018_12_27_052646_create_playlist_table.php b/laravel/database/migrations/2018_12_27_052646_create_playlist_table.php
new file mode 100644
index 0000000..6b68c7f
--- /dev/null
+++ b/laravel/database/migrations/2018_12_27_052646_create_playlist_table.php
@@ -0,0 +1,41 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreatePlaylistTable extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::create('playlists', function (Blueprint $table) {
+ $table->increments('id');
+ $table->text('name');
+ $table->timestamps();
+ });
+ Schema::create('playlist_song', function(Blueprint $table) {
+ $table->increments('id');
+ $table->unsignedInteger('playlist_id');
+ $table->unsignedInteger('song_id');
+ $table->unique(['playlist_id', 'song_id']);
+ $table->foreign('playlist_id')->references('id')->on('playlists');
+ $table->foreign('song_id')->references('id')->on('songs');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('playlist_song');
+ Schema::dropIfExists('playlists');
+ }
+}