I am trying to save a simple belongsToMany
relation into my database. First, my table:
Schema::create('user_activity_log', function (Blueprint $table) {
$table->unsignedBigInteger('activity_id');
$table->unsignedBigInteger('log_id');
$table->unsignedInteger('sets');
$table->unsignedInteger('reps');
$table->primary(['activity_id', 'log_id']);
$table->foreign('activity_id')->references('id')->on('user_activities')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('log_id')->references('id')->on('user_logs')
->onDelete('cascade')
->onUpdate('cascade');
});
My Log
and Activity
models have these relations:
Log:
public function activities()
{
return $this->belongsToMany(Activity::class, 'user_activity_log', 'log_id', 'activity_id')->withPivot(['sets', 'reps']);
}
Activity:
public function logs()
{
return $this->belongsToMany(Log::class, 'user_activity_log', 'activity_id', 'log_id')->withPivot(['sets', 'reps']);
}
When adding a new activity to an existing log, in my store method I am doing simply this:
$log->activities()->syncWithoutDetaching([
$request->input('activity_id') => [
'sets' => $request->input('sets'),
'reps' => $request->input('reps')
]
]);
I am getting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'
select * from `user_activity_log` where `id` = 0 limit 1
The data IS being saved though, so when I refresh the page and thus submit the form again, there are no errors. I cannot figure out why it is trying to find the id
in the user_activity_log
table.
2
Answers
So after some more investigating it seems that it was an issue with the Spatie Activitylog. Nothing with the naming of my models etc though. What the issue is exactly is still being investigated.
So I was using a pivot model "ActivityLog", to log the activity on the pivot table. But I forgot to add a primary key to the
activity_log
table. So this had totally nothing to do withattach
orsyncWithoutDetaching
indeed.There is nothing wrong with the posted code. The problem is somewhere else.