From ecd2d2920f2d94212e026d93541f07499c93ae3c Mon Sep 17 00:00:00 2001 From: Mark Salmon Date: Thu, 9 Sep 2021 14:24:03 +0100 Subject: [PATCH] editable field, update with primary key other than id (#282) * wip * Apply fixes from StyleCI (#281) Co-authored-by: Mark Salmon Co-authored-by: Mark Salmon --- .phpunit.result.cache | 1 + resources/views/livewire/datatables/editable.blade.php | 2 +- src/Http/Livewire/LivewireDatatable.php | 2 +- src/Traits/WithCallbacks.php | 7 ++++--- 4 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 00000000..5bbf4446 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":[],"times":{"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_generate_an_array_of_columns_from_a_model":0.269,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #0":0.015,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #1":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #2":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #3":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #4":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #5":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #6":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_correctly_populate_the_columns_from_the_model with data set #7":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_exclude_columns":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_include_columns":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnSetTest::it_can_rename_columns":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_column_from_a_table_column":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_column_from_a_scope":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_can_generate_a_delete_column":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #0":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #1":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #2":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #3":0.011,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_sets_properties_and_parameters with data set #4":0.012,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_column":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_an_array_from_raw":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\ColumnTest::it_returns_width_property_from_column":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_mount_using_the_class":0.089,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_set_a_default_sort":0.02,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_show_and_hide_a_column":0.043,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_order_results":0.015,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_text":0.028,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_boolean":0.025,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_strings_as_a_boolean":0.028,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_selects":0.026,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableClassTest::it_can_filter_results_based_on_numbers":0.062,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_base_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_one_relation_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_a_has_one_column":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_columns":0.015,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_has_many_relation_column_with_specific_aggregate":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_has_many_relation_columns":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_relation_columns":0.019,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_where_query_for_belongs_to_relation_columns":0.015,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a_query_builder_for_belongs_to_many_relation_columns":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableQueryBuilderTest::it_creates_a__where_query_for_belongs_to_many_relation_columns":0.013,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mount_from_the_default_template_with_a_model":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_header_can_be_hidden_with_a_property":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::the_pagination_bar_can_be_hidden_with_a_property":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_per_page_with_a_property":0.023,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_include_columns_from_a_property":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_exclude_columns_from_a_property":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_hide_columns_from_a_property":0.02,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_date_format_from_a_property":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_mark_columns_for_time_format_from_a_property":0.019,"Mediconesystems\\LivewireDatatables\\Tests\\LivewireDatatableTemplateTest::it_can_set_sort_from_a_property":0.019,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::component_is_created_by_make_command":0.02,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::dot_nested_component_is_created_by_make_command":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::forward_slash_nested_component_is_created_by_make_command":0.016,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::multiword_component_is_created_by_make_command":0.017,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::pascal_case_component_is_automatically_converted_by_make_command":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command":0.014,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::snake_case_component_is_automatically_converted_by_make_command_on_nested_component":0.018,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::new_component_model_name_matches_option":0.015,"Mediconesystems\\LivewireDatatables\\Tests\\MakeDatatableCommandTest::a_component_is_not_created_with_a_reserved_class_name":0.012}} \ No newline at end of file diff --git a/resources/views/livewire/datatables/editable.blade.php b/resources/views/livewire/datatables/editable.blade.php index 8f57e3b9..925cc050 100644 --- a/resources/views/livewire/datatables/editable.blade.php +++ b/resources/views/livewire/datatables/editable.blade.php @@ -16,7 +16,7 @@ x-on:click="edit = true; $nextTick(() => { $refs.input.focus() })">{!! htmlspecialchars($value) !!} diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php index 069aab56..838bb948 100644 --- a/src/Http/Livewire/LivewireDatatable.php +++ b/src/Http/Livewire/LivewireDatatable.php @@ -1315,7 +1315,7 @@ public function mapCallbacks($paginatedCollection, $export = false) } elseif (isset($this->editables[$name])) { $row->$name = view('datatables::editable', [ 'value' => $value, - 'table' => $this->builder()->getModel()->getTable(), + 'key' => $this->builder()->getModel()->getQualifiedKeyName(), 'column' => Str::after($name, '.'), 'rowId' => $row->{$this->builder()->getModel()->getTable() . '.' . $this->builder()->getModel()->getKeyName()} ?? $row->{$this->builder()->getModel()->getKeyName()}, ]); diff --git a/src/Traits/WithCallbacks.php b/src/Traits/WithCallbacks.php index 24f7c509..5a72d64a 100644 --- a/src/Traits/WithCallbacks.php +++ b/src/Traits/WithCallbacks.php @@ -3,13 +3,14 @@ namespace Mediconesystems\LivewireDatatables\Traits; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Str; trait WithCallbacks { - public function edited($value, $table, $column, $rowId) + public function edited($value, $key, $column, $rowId) { - DB::table($table) - ->where('id', $rowId) + DB::table(Str::before($key, '.')) + ->where(Str::after($key, '.'), $rowId) ->update([$column => $value]); $this->emit('fieldEdited', $rowId);