Verified Commit 9506a30c authored by Maxime FRIESS's avatar Maxime FRIESS 💙
Browse files

[db] Use pivot model for people-events relation

parent b0ac1641
Pipeline #65566 passed with stages
in 5 minutes and 18 seconds
...@@ -8,4 +8,29 @@ use Illuminate\Database\Eloquent\Model; ...@@ -8,4 +8,29 @@ use Illuminate\Database\Eloquent\Model;
class Event extends Model class Event extends Model
{ {
use HasFactory; use HasFactory;
protected $fillable = [
'name',
'location',
'inscriptions_closed_at',
'start',
'end',
'max_people',
'price',
'price_member',
'additional_data',
'category_id'
];
public function category()
{
return $this->belongsTo(TransactionCategory::class);
}
// phpcs:ignore
public function events()
{
return $this->belongsToMany(Person::class, 'event_person', 'event_id', 'person_id')
->using(EventPerson::class)->withTimestamps();
}
} }
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class EventParticipation extends Model
{
use HasFactory;
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
class EventPerson extends Pivot
{
use HasFactory;
protected $fillable = [
'event_id',
'person_id',
'transaction_id',
'additional_data'
];
public function event()
{
return $this->belongsTo(Event::class);
}
public function person()
{
return $this->belongsTo(Person::class);
}
public function transaction()
{
return $this->belongsTo(Transaction::class);
}
}
...@@ -43,4 +43,11 @@ class Person extends Model ...@@ -43,4 +43,11 @@ class Person extends Model
{ {
return $this->hasOne(PersonalAccount::class); return $this->hasOne(PersonalAccount::class);
} }
// phpcs:ignore
public function events()
{
return $this->belongsToMany(Event::class, 'event_person', 'person_id', 'event_id')
->using(EventPerson::class)->withTimestamps();
}
} }
...@@ -49,7 +49,7 @@ class CreateEventsTable extends Migration ...@@ -49,7 +49,7 @@ class CreateEventsTable extends Migration
$table->decimal("max_people"); $table->decimal("max_people");
$table->decimal('price', $precision = 12, $scale = 3)->default(0); $table->decimal('price', $precision = 12, $scale = 3)->default(0);
$table->decimal('price_member', $precision = 12, $scale = 3)->default(0); $table->decimal('price_member', $precision = 12, $scale = 3)->default(0);
$table->json("additional_data"); $table->json("data_structure");
$table->foreignId("category_id"); $table->foreignId("category_id");
$table->timestamps(); $table->timestamps();
} }
......
...@@ -6,7 +6,7 @@ use Illuminate\Database\Migrations\Migration; ...@@ -6,7 +6,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
class CreateEventParticipationsTable extends Migration class CreateEventPersonTable extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
...@@ -16,14 +16,14 @@ class CreateEventParticipationsTable extends Migration ...@@ -16,14 +16,14 @@ class CreateEventParticipationsTable extends Migration
public function up() public function up()
{ {
Schema::create( Schema::create(
'event_participations', 'event_person',
function (Blueprint $table) { function (Blueprint $table) {
$table->id();
$table->foreignId("event_id"); $table->foreignId("event_id");
$table->foreignId("person_id"); $table->foreignId("person_id");
$table->foreignId("transaction_id")->nullable()->default(null); $table->foreignId("transaction_id")->nullable()->default(null);
$table->json("additional_data"); $table->json("data");
$table->timestamps(); $table->timestamps();
$table->primary(["event_id", "person_id"]);
} }
); );
} }
...@@ -35,6 +35,6 @@ class CreateEventParticipationsTable extends Migration ...@@ -35,6 +35,6 @@ class CreateEventParticipationsTable extends Migration
*/ */
public function down() public function down()
{ {
Schema::dropIfExists('event_participations'); Schema::dropIfExists('event_person');
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment