$columns * @return Collection */ public function all(array $columns = ['*']): Collection { return $this->model->newQuery()->get($columns); } /** * @param int|string $id * @param array $columns */ public function find(int|string $id, array $columns = ['*']): ?Model { return $this->model->newQuery()->find($id, $columns); } /** * Create a new model instance with transaction support. * * @param array $attributes * @throws Exception */ public function create(array $attributes): Model { try { DB::beginTransaction(); // Uses mass assignment; ensure $fillable is set on the model $model = $this->model->newQuery()->create($attributes); DB::commit(); return $model; } catch (Exception $e) { DB::rollBack(); Log::error('Error creating ' . get_class($this->model) . ': ' . $e->getMessage(), [ 'attributes' => $attributes, 'exception' => $e ]); throw $e; } } /** * Update an existing model instance with transaction support. * * @param int|string $id * @param array $attributes * @throws Exception */ public function update(int|string $id, array $attributes): bool { try { DB::beginTransaction(); $instance = $this->find($id); if (! $instance) { DB::rollBack(); return false; } $result = $instance->fill($attributes)->save(); DB::commit(); return $result; } catch (Exception $e) { DB::rollBack(); Log::error('Error updating ' . get_class($this->model) . ' with ID ' . $id . ': ' . $e->getMessage(), [ 'id' => $id, 'attributes' => $attributes, 'exception' => $e ]); throw $e; } } /** * Delete a model instance with transaction support. * * @param int|string $id * @throws Exception */ public function delete(int|string $id): bool { try { DB::beginTransaction(); $instance = $this->find($id); if (! $instance) { DB::rollBack(); return false; } $result = (bool) $instance->delete(); DB::commit(); return $result; } catch (Exception $e) { DB::rollBack(); Log::error('Error deleting ' . get_class($this->model) . ' with ID ' . $id . ': ' . $e->getMessage(), [ 'id' => $id, 'exception' => $e ]); throw $e; } } }