Where are the event records?
Where are the event recors?
$events = Event::with('Memory')
->where('user_id', '=', Auth::id())
->orderByDesc('id')->get();
Returns this:
1 101 2021-05-05 12:38:46 2021-05-05 12:38:46 1 1 ... 10 memory 103 1 10 103 103 103 103 103 103 103 103 103 103 103 103 10... 2021-05-05 16:31:36 2021-05-05 16:31:36 10 10
The query that eloquent created is:
select memories.*, event_memory.event_id as pivot_event_id, event_memory.memory_id as pivot_memory_id from memories inner join event_memory on memories.id = event_memory.memory_id where event_memory.event_id in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52)
I do not see any Event records. Why not? The tabels are: Schema::create('events', function (Blueprint $table) { $table->increments('id'); $table->string('eventtitle')->unique(); $table->string('eventbody'); $table->integer('memory_id')->unsigned()->index()->nullable(); $table->foreign('memory_id')->references('id')->on('memories'); $table->integer('user_id')->unsigned()->index()->nullable(); $table->foreign('user_id')->references('id')->on('users'); $table->integer('family_id')->unsigned()->index()->nullable(); $table->foreign('family_id')->references('id')->on('families'); $table->string('memTitle')->nullable(); $table->string('memBody')->nullable(); $table->timestamps(); });
Schema::create('memories', function (Blueprint $table) {
$table->id();
$table->string('memtitle')->unique();
$table->integer('user_id')->unsigned()->index()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('event_id')->unsigned()->index()->nullable();
$table->foreign('event_id')->references('id')->on('events');
$table->text('membody');
$table->timestamps();
});
The Models are: class Memory extends Model { use HasFactory; protected $fillable = [ 'user_id', 'title', 'body', 'event_id', 'published_at' ];
public function event()
{
return $this->hasOne(Event::class);
}
}
class Event extends Model
{ use HasFactory; protected $fillable = [ 'user_id', 'eventtitle', 'eventbody', 'family_id', 'memory_id', 'memtitle', 'membody' ];
public function memory()
{
return $this->belongsToMany(Memory::class);
}
}
This query returns the correct data: select e., m. from events e inner join event_memory em on em.event_id = e.id inner join memories m on m.id = em.memory_id where e.id in (select e1.event_id from event_memory e1)
What do I need to change?
Thanks...Dan'lt records?
Please or to participate in this conversation.