So, I have an events table, where users can 'go' or be 'interested', and many users can go. (For this example, we'll just use it on the Events class.
So, I am specifying the morph like so:
/**
* Get the user's events they're interested in
*
* @return builder
*/
public function interested($class = __CLASS__)
{
return $this->morphToMany($class, 'owner', EventParticipants::table)
->wherePivot('status', '=', EventParticipants::INTERESTED)
->withPivot('owner_type', 'status', 'created_at');
}
Which is a trait inside my users model. I can have multiple statuses (going, interested..)
Now, my database structure is like so:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('event_participants', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('owner_id');
$table->string('owner_type');
$table->string('status')->default('pending')->comment('pending/going/interested');
$table->timestamps();
});
}
And my example data:
INSERT INTO `social`.`event_participants`
(`id`,
`user_id`,
`owner_type`,
`owner_id`,
`status`,
`created_at`,
`updated_at`)
VALUES
('14',
'9',
'App\\Modules\\Events\\Entities\\Events',
'1',
'2',
'2018-01-27 14:19:03',
'2018-01-27 14:19:03');
But the morph trait, is not using my specified owner
key, from the documentation is specifies the class to morph, the key, and the table (optional)
relationships: https://laravel.com/docs/5.5/eloquent-relationships
how come my code isn't abiding by he rules? How can I fix this?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire