i have many to many relation in my project tasks and offices table :
task model:
public function offices(): BelongsToMany
{
return $this->belongsToMany(Office::class)->withPivot('main_office');
}
office model:
public function tasks(): BelongsToMany
{
return $this->belongsToMany(Task::class)->withPivot('main_office');
}
in store function in controller:
public function store(taskRequestStore $request,TaskService $service)
{
$offices=Office::where([['top_office_id',null],['status',1]])->get();
$attributes = $request->all(['Caption','TaskDsc','TaskDate','Priority_id',
'Type','TaskFile','FilePath','StartDate','EndDate','Deadline',
'Commander_id','Status_id'
]);
$tasks = $service->create($attributes);
// dd($tasks,$offices);
$tasks->$offices->attach([1]);
return redirect()->back();
}
The record is inserted in the tasks table, but an error is issued for the attach command.
when active dd :
Where is the error in my code?