And lastly, move recurrings from users to spaces
MODIFY
app/Http/Controllers/RecurringController.php
app/Http/Controllers/RecurringController.php
@@ -13,7 +13,7 @@ class RecurringController extends Controller {
return view('recurrings.index', [
'currency' => $user->currency,
- 'recurrings' => $user->recurrings()->orderBy('created_at', 'DESC')->get()
+ 'recurrings' => session('space')->recurrings()->orderBy('created_at', 'DESC')->get()
]);
}
@@ -24,10 +24,8 @@ class RecurringController extends Controller {
}
public function create() {
- $user = Auth::user();
-
return view('recurrings.create', [
- 'tags' => $user->tags
+ 'tags' => session('space')->tags
]);
}
@@ -44,7 +42,7 @@ class RecurringController extends Controller {
$recurring = new Recurring;
- $recurring->user_id = $user->id;
+ $recurring->space_id = session('space')->id;
$recurring->type = 'monthly';
$recurring->day = $request->input('day');
$recurring->starts_on = date('Y-m-d');

MODIFY
app/Jobs/ProcessRecurrings.php
app/Jobs/ProcessRecurrings.php
@@ -31,7 +31,7 @@ class ProcessRecurrings implements ShouldQueue {
foreach ($recurrings as $recurring) {
$spending = new Spending;
- $spending->user_id = $recurring->user_id;
+ $spending->space_id = $recurring->space_id;
$spending->recurring_id = $recurring->id;
$spending->tag_id = $recurring->tag_id;
$spending->happened_on = date('Y-m-d');

MODIFY
app/Space.php
app/Space.php
@@ -22,4 +22,8 @@ class Space extends Model {
public function spendings() {
return $this->hasMany(Spending::class);
}
+
+ public function recurrings() {
+ return $this->hasMany(Recurring::class);
+ }
}

MODIFY
app/User.php
app/User.php
@@ -20,10 +20,6 @@ class User extends Authenticatable {
return $this->belongsToMany(Space::class, 'user_space');
}
- public function recurrings() {
- return $this->hasMany(Recurring::class);
- }
-
public function currency() {
return $this->belongsTo(Currency::class);
}

MODIFY
database/migrations/2018_09_13_112448_create_recurrings_table.php
database/migrations/2018_09_13_112448_create_recurrings_table.php
@@ -8,7 +8,7 @@ class CreateRecurringsTable extends Migration {
public function up() {
Schema::create('recurrings', function (Blueprint $table) {
$table->increments('id');
- $table->integer('user_id')->unsigned();
+ $table->unsignedInteger('space_id');
$table->string('type');
$table->string('day');
$table->date('starts_on');

And lastly, move recurrings from users to spaces