public function store(Request $request) { if (!$request->ajax()) { return back(); } $collection = Collection::find($request->collection_id); $collection->posts()->attach($request->post_id); return response()->json(['name' => $request->post_id, 'state' => '4']); }