Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table Class Exports with a Request value #444

Open
EpikkuH opened this issue Jun 7, 2023 · 3 comments
Open

Table Class Exports with a Request value #444

EpikkuH opened this issue Jun 7, 2023 · 3 comments

Comments

@EpikkuH
Copy link

EpikkuH commented Jun 7, 2023

  • Laravel Version: 10.13.1
  • PHP Version: 8.2.0
  • Splade JS Version (npm): 1.4.11
  • Splade PHP Version (composer): 1.4.11
  • Dev environment (OS, Sail/Valet/etc): Windows

Description:

I am using the table class because i need to do exports but i am having problems because i need a $id for the query which i get from the request and it is lost by the time i export the table someone haves any idea how i could approach this?

Steps To Reproduce Issue:

on my table class:

    public function __construct(Request $request)
    {
        $this->id = $request->id;
    }

    public function for()
    {
        return MovimientosDeInventario::query()
            ->join('productos','productos.id', '=','product_id')
            ->join('ordenes_de_entradas','ordenes_de_entradas.id', '=','order_id')
            ->select('movimientos_de_inventarios.*', 'productos.nombre','ordenes_de_entradas.order_key')
            ->where('product_id','=',$this->id);
    }

everything works except the export which returns an empty table instead of the same one it originally shows

@jamesj2
Copy link
Contributor

jamesj2 commented Jun 28, 2023

I suggest trying it without the ->where('product_id','=',$this->id); in your query.

...edit
Sorry, I didn't pay attention enough attention to your issue description :(. Have you looked at the request going to the server? I probably not passing the id. And I don't see where that is configurable.

@EpikkuH
Copy link
Author

EpikkuH commented Jul 5, 2023

I suggest trying it without the ->where('product_id','=',$this->id); in your query.

...edit Sorry, I didn't pay attention enough attention to your issue description :(. Have you looked at the request going to the server? I probably not passing the id. And I don't see where that is configurable.

It's alright yeah it seems the id gets lost and i don't see any way to make it work at least for now, if it's not possible right know it definitely should be in the future thanks for your time anyways

@Adel-Kazem
Copy link

I have fixed this issue by editing the core files of the Splade.

Here is step by step how I have successfully was able to send the ID that is associated with a relationship with the records inside the data table:

Step 1: Define Extra Parameters in BulkAction Class
Add a method in the BulkAction class to set extra parameters.
// In BulkAction.php public function setExtraParameters(array $extraParameters) { $this->extraParameters = $extraParameters; }

Step 2: Set Extra Parameters in HasBulkActions Trait
When defining a bulk action, set the extra parameters.
`// In HasBulkActions.php
public function bulkAction(
// ... other parameters,
array $extraParameters = []
): self {
// ... existing code to set up bulk action

$this->bulkActions[$key]->setExtraParameters($extraParameters);

return $this;

}
`

Step 3: Pass Product ID as an Extra Parameter
In your ProductReviews table configuration, pass the productId.
// In ProductReviews.php $table->bulkAction( label: 'Delete Selected', // ... other parameters, extraParameters: ['productId' => $this->productId] );

Step 4: Access Product ID in the Bulk Action Method
Retrieve the productId within the deleteSelectedReviews method using the request helper.
// In ProductReviews.php protected function deleteSelectedReviews($selectedIds) { $productId = request('wildId'); // ... existing deletion logic }

Step 5: Use Extra Parameters in Blade Template
Modify the Blade template to use the extra parameters in the getUrl method.
{{-- In your Blade template --}} @foreach($table->getBulkActions() as $bulkAction) <button v-if="table.hasSelectedItems" @click.prevent="table.performBulkAction( @js($bulkAction->getUrl(['wildId' => $bulkAction->extraParameters])), // ... other parameters )" dusk="action.{{ $bulkAction->getSlug() }}" > {{ $bulkAction->label }} </button> @endforeach

These steps ensure that the productId is passed as an additional parameter when defining bulk actions and is accessible within the method that performs the bulk deletion. The use of @js in the Blade template ensures the JavaScript code has the correct parameters passed from the server side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants