pluck('column1'); // Column 1 for X-axis $values = $data->pluck('column3'); // Column 3 for Y-axis // Return the view with the data and graph data return view('import', compact('data', 'labels', 'values')); } // Handle the import of CSV/Excel file public function import(Request $request) { // Validate the uploaded file $request->validate([ 'file' => 'required|mimes:csv,txt', ]); // Get the uploaded file $file = $request->file('file'); // Convert the file data to an array $rows = Excel::toArray(new DataImport, $file); // Preprocess the CSV data (e.g., clean invalid rows) $validRows = collect($rows[0])->filter(function ($row) { return is_numeric($row[2]); // Only include rows where column3 is numeric }); // Insert valid rows into the database foreach ($validRows as $row) { Data::create([ 'column1' => $row[0], // Assuming column1 contains a value for the first column 'column2' => $row[1], // Assuming column2 contains a value for the second column 'column3' => $row[2], // Assuming column3 contains a value for the third column ]); } // After the import, redirect back with a success message return back()->with('success', 'Data Imported Successfully'); } // Show the graph with data public function showGraph() { // Fetch the data from the database $data = Data::all(); // Prepare the data for the chart $labels = $data->pluck('column1'); // Use column1 as labels $values = $data->pluck('column3'); // Use column3 as the values for the chart // Return the data to the Blade view return view('graph', compact('labels', 'values', 'data')); } // Delete a data entry by ID public function destroy($id) { // Find the data record by ID $data = Data::findOrFail($id); // Delete the record $data->delete(); // Return a success response for the deletion return response()->json(['message' => 'Data deleted successfully']); } public function destroyAll() { // Delete all records from the Data model Data::delete(); // This deletes all records in the Data table // Redirect back to the import form with a success message return back()->with('success', 'All data deleted successfully'); } }