success( $assistant->tools()->get() ); } /** * Store a newly created resource in storage. */ public function store(AssistantRequest $request, Assistant $assistant) { $request->validate([ 'tool_id' => 'required|exists:tools,id', ]); $tool = Tool::find($request->input('tool_id')); // 检测 tool是否属于user if ($tool->user_id !== $request->user('api')->id) { return $this->forbidden(); } // 检测是否存在 if ($assistant->tools()->where('tool_id', $tool->id)->exists()) { return $this->conflict('The tool already exists'); } $assistant->tools()->attach($tool->id); return $this->created($assistant->tools); } /** * Remove the specified resource from storage. */ public function destroy(AssistantRequest $request, Assistant $assistant, Tool $tool) { // 从 assistant_tools 表中删除 $assistant->tools()->detach($tool->id); return $this->deleted(); } }