I've been trying to set up a many-to-many pivot table between meetings and attendees. When I try to call the relationship function in MeetingForm.php
$this->attendees = $meeting->attendees();
I get the following error
Property type not supported in Livewire for property: [{"withTimestamps":false}]
I've tried also including timestamps in my migration but it makes no difference and I'd rather not have to include them, is there a way around this or is it not possible to do what I want to do?
Attendee.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Attendee extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = ['name', 'email'];
/**
* Get the meetings for the attendee.
*
* @return BelongsToMany
*/
public function meetings(): BelongsToMany
{
return $this->belongsToMany(Meeting::class);
}
}
Meeting.php
namespace App\Models;
use App\Enums\MeetingStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Meeting extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = ['title', 'meeting_time', 'status'];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'status' => MeetingStatus::class,
];
}
/**
* Get the attendees for the meeting.
*
* @return BelongsToMany
*/
public function attendees(): BelongsToMany
{
return $this->belongsToMany(Attendee::class);
}
}
attendee_meeting migration
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('attendee_meeting', function (Blueprint $table) {
$table->unsignedBigInteger('attendee_id');
$table->unsignedBigInteger('meeting_id');
$table->unique(['attendee_id', 'meeting_id']);
$table->foreign('attendee_id')->references('id')->on('attendees')->onDelete('cascade');
$table->foreign('meeting_id')->references('id')->on('meetings')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('attendee_meeting');
}
};
MeetingForm.php
<?php
namespace App\Livewire\Forms;
use App\Models\Meeting;
use Livewire\Attributes\Validate;
use Livewire\Form;
class MeetingForm extends Form
{
public ?Meeting $meeting;
#[Validate('required|min:3')]
public $title = '';
#[Validate('required|date')]
public $meeting_time = '';
public $attendees;
public function setMeeting(Meeting $meeting)
{
$this->meeting = $meeting;
$this->title = $meeting->title;
$this->meeting_time = $meeting->meeting_time;
$this->attendees = $meeting->attendees();
}
public function sanitiseMeetingTime()
{
//Sanitise meeting date and time to be stored in the db
$meetingTimeExplode = explode('T', $this->meeting_time);
$meetingTimeExplode[1] .= ':00';
$this->meeting_time = implode(' ', $meetingTimeExplode);
}
public function addAttendeeToMeeting($attendee)
{
}
public function store()
{
$this->validate();
//Sanitise meeting date and time to be stored in the db
$this->sanitiseMeetingTime();
Meeting::create($this->only(['title', 'meeting_time']));
}
public function update()
{
$this->validate();
//Sanitise meeting date and time to be stored in the db
$this->sanitiseMeetingTime();
$this->meeting->update(
$this->all()
);
}
}