The code looks good, but there is a simpler way to handle the default material. Instead of querying the database twice, you can use the value method to get the default material ID directly. Here's how you can modify the index method:
public function index()
{
$materials = Material::where('active', 1)->get();
$default_material_id = Material::where('default', 1)->value('id');
return view('weight.tube', compact('materials', 'default_material_id'));
}
This will set the $default_material_id variable to the ID of the default material, or null if there is no default material. You can then use this variable in your Blade template to mark the selected option in the dropdown list.
Also, in the store method, you can simplify the code that sets the $data variable using the only method:
$data = collect($request->only(['inside_diameter', 'outside_diameter', 'length']));
$data['density'] = $material->density;
$data['cross_section'] = $cross_section;
$data['grams'] = $weight;
This will create a new collection with only the specified keys from the $request object.